Add folder in Amazon s3 bucket

前端 未结 16 1379
青春惊慌失措
青春惊慌失措 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: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

提交回复
热议问题