简体   繁体   中英

How to upload a file to AWS S3 subfolder using python 3.8?

I want to upload a file to a subfolder in a Bucket in AWS S3. I am using the code given below.

filename  = '/tmp/' + 'image' + '.png'
file_obj = open(filename, 'rb')
s3_upload = s3.put_object( Bucket="aaa/Bbb/cc-cc", Key="filename.png", Body=file_obj)
   
return
    {
    'statusCode': 200,
    'body': json.dumps("Executed Successfully"
    }

The above concept of uploading files to a subfolder of AWS S3 Bucket works fine in Node.js but when implemented in python then it gives the following error.

Parameter validation failed:\nInvalid bucket name \"aaa/Bbb/cc-cc\": Bucket name must match the regex \"^[a-zA-Z0-9.\\-_]{1,255}$\" or be an ARN matching the regex \"^arn:(aws).*:s3:[a-z\\-0-9]+:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\\-]{1,63}$|^arn:(aws).*:s3-outposts:[a-z\\-0-9]+:[0-9]{12}:outpost[/:][a-zA-Z0-9\\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\\-]{1,63}$\"

在您的情况下,存储桶名称只是aaa ,而/Bbb/cc-cc应该是键名的一部分:

s3_upload = s3.put_object( Bucket="aaa", Key="/Bbb/cc-cc/filename.png", Body=file_obj)

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