Add folder in Amazon s3 bucket

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

    With aws cli, it is possible to copy an entire folder to a bucket.

    aws s3 cp /path/to/folder s3://bucket/path/to/folder --recursive
    

    There is also the option to sync a folder using aws s3 sync

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

    Here's how you can achieve what you're looking for (from code/cli):

    --create/select the file (locally) which you want to move to the folder:
    
    ~/Desktop> touch file_to_move
    
    --move the file to s3 folder by executing:
    
    ~/Desktop> aws s3 cp file_to_move s3://<path_to_your_bucket>/<new_folder_name>/
    

    A new folder will be created on your s3 bucket and you'll now be able to execute cp, mv, rm ... statements i.e. manage the folder as usual.

    If this new file created above is not required, simply delete it. You now have an s3 bucket created.

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

    In iOS (Objective-C), I did following way

    You can add below code to create a folder inside amazon s3 bucket programmatically. This is working code snippet. Any suggestion Welcome.

    -(void)createFolder{
        AWSS3PutObjectRequest *awsS3PutObjectRequest = [AWSS3PutObjectRequest new];
        awsS3PutObjectRequest.key = [NSString stringWithFormat:@"%@/", @"FolderName"];
        awsS3PutObjectRequest.bucket = @"Bucket_Name";
        AWSS3 *awsS3 = [AWSS3 defaultS3];
        [awsS3 putObject:awsS3PutObjectRequest completionHandler:^(AWSS3PutObjectOutput * _Nullable response, NSError * _Nullable error) {
            if (error) {
                NSLog(@"error Creating folder");
            }else{
                NSLog(@"Folder Creating Sucessful");
            }
        }];
    }
    
    0 讨论(0)
  • 2021-02-06 23:46

    With AWS SDK .Net works perfectly, just add "/" at the end of the name folder:

    var folderKey =  folderName + "/"; //end the folder name with "/"
    AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKey, AWSSecretKey);
    var request = new PutObjectRequest();
    request.WithBucketName(AWSBucket);
    request.WithKey(folderKey);
    request.WithContentBody(string.Empty);
    S3Response response = client.PutObject(request);
    

    Then refresh your AWS console, and you will see the folder

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

    You can select language of your choice from available AWS SDK

    Alternatively you can try minio client libraries available in Python, Go, .Net, Java, Javascript for your application development environment, it has example directory with all basic operations listed.

    Disclaimer: I work for Minio

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

    As everyone has told you, in AWS S3 there aren't any "folders", you're thinking of them incorrectly. AWS S3 has "objects", these objects can look like folders but they aren't really folders in the fullest sense of the word. If you look for creating folders on the Amazon AWS S3 you won't find a lot of good results.

    There is a way to create "folders" in the sense that you can create a simulated folder structure on the S3, but again, wrap your head around the fact that you are creating objects in S3, not folders. Going along with that, you will need the command "put-object" to create this simulated folder structure. Now, in order to use this command, you need the AWS CLI tools installed, go here AWS CLI Installation for instructions to get them installed.

    The command is this:

    aws s3api put-object --bucket your-bucket-name --key path/to/file/yourfile.txt --body yourfile.txt
    

    Now, the fun part about this command is, you do not need to have all of the "folders" (objects) created before you run this command. What this means is you can have a "folder" (object) to contain things, but then you can use this command to create the simulated folder structure within that "folder" (object) as I discussed earlier. For example, I have a "folder" (object) named "importer" within my S3 bucket, lets say I want to insert sample.txt within a "folder" (object) structure of the year, month, and then a sample "folder" (object) within all of that.

    If I only have the "importer" object within my bucket, I do not need to go in beforehand to create the year, month, and sample objects ("folders") before running this command. I can run this command like so:

    aws s3api put-object --bucket my-bucket-here --key importer/2016/01/sample/sample.txt --body sample.txt
    

    The put-object command will then go in and create the path that I have specified in the --key flag. Here's a bit of a jewel: even if you don't have a file to upload to the S3, you can still create objects ("folders") within the S3 bucket, for example, I created a shell script to "create folders" within the bucket, by leaving off the --body flag, and not specifying a file name, and leaving a slash at the end of the path provided in the --key flag, the system creates the desired simulated folder structure within the S3 bucket without inserting a file in the process.

    Hopefully this helps you understand the system a little better.

    Note: once you have a "folder" structure created, you can use the S3's "sync" command to syncronize the descendant "folder" with a folder on your local machine, or even with another S3 bucket.

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