简体   繁体   中英

How to upload file to folder in aws S3 bucket using python boto3

How to access a folder object inside S3 bucket. How can I access a folder inside S3 bucket using python boto3. Code is working for a folder in S3 bucket but to for folders inside S3 bucket

Little principle info: S3 does not have folders, it is object store. The folder like structure which you see in the UI is for convenience.

For the upload see here

If I understand you correctly.. I had this issue in my python3 script. Basically you need to pass the to the boto3 function an s3 bucket and the file name. Make this file name include the folder, with the forward slash separating them. Instead of passing just the file name and trying to pass the folder as a separate parameter.

So if you have MyS3Bucket and you want to upload file.txt to MyFolder inside MyS3Bucket, then pass the file_name=“MyFolder”+”/“+”file.txt” as a parameter to the upload function. Let me know if you need a code snippet.

Even if you don't have the folder in the S3 bucket, boto3 will create it for you on the fly. This is cool because you can grant access in s3 based on a folder, not just the whole bucket at once.

Good luck!

If I wanted to upload the following directory:

folder_root
├── a
│   └── f
├── b
│   ├── d
│   └── e
└── c
    └── x

You will want to upload individual files called a/f , b/d , b/e , and c/x . As the other answer states, there is no concept of a directory within S3, it is a "flat object store". If you want to create the appearance of directories, the names (or keys ) of the files must be the directory structure you're trying to create.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM