Creating directories in Amazon S3 using python, boto3

I know that S3 buckets really have no directories because the storage is flat. But you can create directories programmatically using python / boto3, but I don't know how to do this. I saw this in a documentary:

“Although the S3 storage is flat: there are keys in the buckets, S3 allows you to overlay the directory tree structure on your bucket using the separator in your keys. For example, if you name the key 'a / b / f and use' / as a separator, then S3 will be assume that 'a is a directory,' b is a subdirectory of 'a, and' f is a file in 'b. "

I can only create files in the S3 bucket:

self.client.put_object(Bucket=bucketname,Key=filename) 

but I do not know how to create a directory.

+11
python amazon-s3 amazon-web-services boto3
source share
3 answers

A slight modification to the key name is required. self.client.put_object(Bucket=bucketname,Key=filename)

it should be changed to

self.client.put_object(Bucket=bucketname,Key=directoryname/filename)

That's all.

+14
source share

If you are reading the API documentation, you should do it.

 import boto3 s3 = boto3.client("s3") BucketName = "mybucket" myfilename = "myfile.dat" KeyFileName = "/a/b/c/d/{fname}".format(fname=myfilename) with open(myfilename) as f : object_data = f.read() client.put_object(Body=object_data, Bucket=BucketName, Key=KeyFileName) 

Honestly, this is not a “real directory”, but a string preformat structure for an organization.

+12
source share

Adding a slash / to the end of the key name to create the directory did not help me:

 client.put_object(Bucket="foo-bucket", Key="test-folder/") 

You must provide the Body parameter to create the directory:

 client.put_object(Bucket='foo-bucket',Body='', Key='test-folder/') 

Source: ryantuck in boto3 release

0
source share

All Articles