Steve Ritz
Steve Ritz

Reputation: 2287

Create directories in Amazon S3 using python, boto3

I know S3 buckets not really have directories because the storage is flat. But it is possible to create directories programmaticaly with python/boto3, but I don't know how. I saw this on a documentary :

"Although S3 storage is flat: buckets contain keys, S3 lets you impose a directory tree structure on your bucket by using a delimiter in your keys. For example, if you name a key ‘a/b/f’, and use ‘/’ as the delimiter, then S3 will consider that ‘a’ is a directory, ‘b’ is a sub-directory of ‘a’, and ‘f’ is a file in ‘b’."

I can create just files in the a S3 Bucket by :

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

but I don't know how to create a directory.

Upvotes: 22

Views: 35669

Answers (3)

azzamsa
azzamsa

Reputation: 2115

Adding forward slash / to the end of key name, to create directory didn't work for me:

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

You have to supply Body parameter in order to create directory:

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

Source: ryantuck in boto3 issue

Upvotes: 9

mootmoot
mootmoot

Reputation: 13166

If you read the API documentation You should be able to do this.

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, it is not a "real directory", but preformat string structure for organisation.

Upvotes: 16

Hardeep Singh
Hardeep Singh

Reputation: 1425

Just a little modification in key name is required. self.client.put_object(Bucket=bucketname,Key=filename)

this should be changed to

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

Thats all.

Upvotes: 25

Related Questions