J4ce
J4ce

Reputation: 315

upload file to amazon cloud subfolder using python boto3

I'm writing some python script and I am trying to upload a file to the amazon cloud using boto3. The problem is I want to upload the file to a particular subfolder...in some cases I will need to upload the file to a subfolder of a subfolder.

I'm trying to do this:

s3.meta.client.upload_file( "C:\\Users...\\folder1" + "\\" + someFile.txt, "folder/subfolder1/subfolder2", someFile.txt)

I get the following error message:

Invalid bucket name "...": Bucket name must match the regex "^[a-zA-Z0-9. \-]{1,255}$"

It works if I just do folder, but not if I try to do folder/subfolder1/subfolder2.

I tried to understand it with the documentation but could not. Can someone please explain it to me?

Thanks

Upvotes: 5

Views: 5880

Answers (1)

spg
spg

Reputation: 9847

The second parameter to your s3.meta.client.upload_file() call should be the bucket name, not a file path (reference):

s3.meta.client.upload_file( "C:\Users...\folder1" + "\" + someFile.txt, "your-bucket-name", "someFile.txt")

As for folders, you must determine the folder structure using the key (the 3rd param in upload_file()) parameter:

s3.meta.client.upload_file( "C:\Users...\folder1" + "\" + someFile.txt, "your-bucket-name", "some_folder/some_subfoler/someFile.txt")

Upvotes: 10

Related Questions