当存储桶名称具有/分隔符时,如何使用boto3包将文件上传到Amazon S3

时间:2019-04-02 13:48:44

标签: python-3.x amazon-s3 file-upload boto3 bucket

我正在尝试将文件上传到提供商设置的Amazon S3存储桶。问题是我们的存储桶的名称是 -/ provider-folder /我的公司名称- 并且我需要上传以下密钥

/ provider-folder / company-name / date / file-path

我尝试使用prefix和objects.filter解决定界符问题,但我尝试过的所有组合都不起作用。

import boto3
from boto.s3.key import Key
session = boto3.Session(aws_access_key_id=AWS_ACCESS_KEY_ID, 
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
bucket_name = 'provider-folder'
prefix = 'company-name'
s3 = session.resource('s3')
bucket = s3.Bucket(bucket_name)
objs = bucket.objects.filter(Prefix=prefix, delimiter = '/')

s3.meta.client.upload_file('file-path', 
                           objs, 'date/file-path')

我也尝试了以下方法:


import boto3
from boto.s3.key import Key
session = boto3.Session(aws_access_key_id=AWS_ACCESS_KEY_ID, 
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
bucket_name = 'company-name'
prefix = 'provider-folder/date'
s3 = session.resource('s3')
bucket = s3.Bucket(bucket_name)
objs = bucket.objects.filter(Prefix=prefix, delimiter = '/')

s3.meta.client.upload_file('file-path', 
                           bucket, 'prefix/file-path')

这也是:


import boto3
from boto.s3.key import Key
session = boto3.Session(aws_access_key_id=AWS_ACCESS_KEY_ID, 
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
bucket_name = 'provider-folder/company-name'
prefix = 'date'
s3 = session.resource('s3')
bucket = s3.Bucket(bucket_name)
objs = bucket.objects.filter(Prefix=prefix, delimiter = '/')

s3.meta.client.upload_file('file-path', 
                           objs, 'file-path')

全部返回相同的错误

 220             'Invalid bucket name "%s": Bucket name must match '

TypeError: expected string or bytes-like object

我现在不确定要作为存储桶名称,前缀和密钥传递什么。

0 个答案:

没有答案