我是 AWS 的新手,我使用 boto3 将文件上传到 s3。但是我在配置 boto3 连接时有点困惑。下面是代码。
import boto3
s3 = boto3.resource(
's3',
aws_access_key_id='access_key',
aws_secret_access_key='secret_key'
)
data = open('test.txt', 'rb')
s3.Bucket('bucketname').put_object(Key='test.txt', Body=data)
I tried to print the bucket names using below code and it worked well.
for bucket in s3.buckets.all():
print(bucket.name)
但是如果我给 bucketname s3.Bucket('xxxxxx').put_object(Key='test.txt', Body=data)
我得到的错误是:
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid bucket name "xxxxxx": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$"
我需要这方面的帮助。
请您参考如下方法:
如果你的桶是s3://my-bucket-x/ , 然后使用 my-bucket-x boto3 中的存储桶名称.
