可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
How can I create a folder under a bucket using boto
library for Amazon s3?
I followed the manual, and created the keys with permission, metadata etc, but no where in the boto's documentation it describes how to create folders under a bucket, or create a folder under folders in bucket.
回答1:
There is no concept of folders or directories in S3. You can create file names like "abc/xys/uvw/123.jpg"
, which many S3 access tools like S3Fox
show like a directory structure, but it's actually just a single file in a bucket.
回答2:
Assume you wanna create folder abc/123/ in your bucket, it's a piece of cake with Boto
k = bucket.new_key('abc/123/') k.set_contents_from_string('')
Or use the console
回答3:
With AWS SDK .Net works perfectly, just add "/" at the end of the folder name string:
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
回答4:
Append "_$folder$" to your folder name and call put.
String extension = "_$folder$"; s3.putObject("MyBucket", "MyFolder"+ extension, new ByteArrayInputStream(new byte[0]), null);
see: http://www.snowgiraffe.com/tech/147/creating-folders-programmatically-with-amazon-s3s-api-putting-babies-in-buckets/
回答5:
S3 doesn't have a folder structure, But there is something called as keys.
We can create /2013/11/xyz.xls
and will be shown as folder's in the console. But the storage part of S3 will take that as the file name.
Even when retrieving we observe that we can see files in particular folder (or keys) by using the ListObjects
method and using the Prefix
parameter.
回答6:
It's really easy to create folders. Actually it's just creating keys.
You can see my below code i was creating a folder with utc_time as name.
Do remember ends the key with '/' like below, this indicates it's a key:
Key='folder1/' + utc_time + '/'
client = boto3.client('s3') utc_timestamp = time.time() def lambda_handler(event, context): UTC_FORMAT = '%Y%m%d' utc_time = datetime.datetime.utcfromtimestamp(utc_timestamp) utc_time = utc_time.strftime(UTC_FORMAT) print 'start to create folder for => ' + utc_time putResponse = client.put_object(Bucket='mybucketName', Key='folder1/' + utc_time + '/') print putResponse
回答7:
Apparently you can now create folders in S3. I'm not sure since when, but I have a bucket in "Standard" zone and can choose Create Folder from Action dropdown.
回答8:
Although you can create a folder by appending "/" to your folder_name. Under the hood, S3 maintains flat structure unlike your regular NFS.
var params = { Bucket : bucketName, Key : folderName + "/" }; s3.putObject(params, function (err, data) {});
回答9:
Use this:
import boto3 s3 = boto3.client('s3') bucket_name = "YOUR-BUCKET-NAME" directory_name = "DIRECTORY/THAT/YOU/WANT/TO/CREATE" #it's name of your folders s3.put_object(Bucket=bucket_name, Key=(directory_name+'/'))