Add folder in Amazon s3 bucket

前端 未结 16 1338
青春惊慌失措
青春惊慌失措 2021-02-06 23:34

I want to add Folder in my amazon s3 bucket using coding. Can you please suggest me how to achieve this?

相关标签:
16条回答
  • 2021-02-06 23:49

    Java with AWS SDK:

    1. There are no folders in s3, only key/value pairs. The key can contain slashes (/) and that will make it appear as a folder in management console, but programmatically it's not a folder it is a String value.

    2. If you are trying to structure your s3 bucket, then your naming conventions (the keys you give your files) can simply follow normal directory patterns, i.e. folder/subfolder/file.txt.

      When searching (depending on language you are using), you can search via prefix with a delimiter. In Java, it would be a listObjects(String storageBucket, String prefix, String delimiter) method call.

      The storageBucket is the name of your bucket, the prefix is the key you want to search, and the delimiter is used to filter your search based off the prefix.

    0 讨论(0)
  • 2021-02-06 23:52

    The AWS:S3 rails gem does this by itself:

    AWS::S3::S3Object.store("teaser/images/troll.png", file, AWS_BUCKET)
    

    Will automatically create the teaser and images "folders" if they don't already exist.

    0 讨论(0)
  • 2021-02-06 23:55

    Below creates a empty directory called "mydir1".

    Below is nodejs code, it should be similar for other languages.

    The trick is to have slash (/) at the end of the name of object, as in "mydir1/", otherwise a file with name "mydir1" will be created.

    let AWS = require('aws-sdk');
    AWS.config.loadFromPath(__dirname + '\\my-aws-config.json');
    let s3 = new AWS.S3();
    
    var params = {
        Bucket: "mybucket1",
        Key: "mydir1/",
        ServerSideEncryption: "AES256" };
    
    s3.putObject(params, function (err, data) {
        if (err) {
            console.log(err, err.stack); // an error occurred
            return;
        } else {
            console.log(data);           // successful response
            return;
            /*
             data = {
             ETag: "\"6805f2cfc46c0f04559748bb039d69ae\"",
             ServerSideEncryption: "AES256",
             VersionId: "Ri.vC6qVlA4dEnjgRV4ZHsHoFIjqEMNt"
             }
             */
        } });
    

    Source: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property

    0 讨论(0)
  • 2021-02-06 23:58

    There are no folders in Amazon S3. It just that most of the S3 browser tools available show part of the key name separated by slash as a folder.

    If you really need that you can create an empty object with the slash at the end. e.g. "folder/" It will looks like a folder if you open it with a GUI tool and AWS Console.

    0 讨论(0)
  • 2021-02-06 23:58

    In swift 2.2 you can create folder using

    func createFolderWith(Name: String!) {
        let folderRequest: AWSS3PutObjectRequest = AWSS3PutObjectRequest()
        folderRequest.key = Name + "/"
        folderRequest.bucket = "Your Bucket Name"
        AWSS3.defaultS3().putObject(folderRequest).continueWithBlock({ (task) -> AnyObject? in
            if task.error != nil {
                assertionFailure("* * * error: \(task.error?.localizedDescription)")
            } else {
                print("created \(Name) folder")
            }
            return nil
        })
    }
    
    0 讨论(0)
  • 2021-02-07 00:01

    I guess your query is just simply creating a folder inside folder(subfolder). so while coping any directory data inside a bucket sub folder use command like this.

    aws s3 cp mudit s3://mudit-bucket/Projects-folder/mudit-subfolder --recursive

    It will create a subfolder and put ur directory contents in it. Also once your subfolder data gets empty. Your Subfolder will automatically gets deleted.

    0 讨论(0)
提交回复
热议问题