gpt4 book ai didi

python - 类型错误 : expected string or bytes-like object - how to copy files to an S3 bucket using the code provided in AWS's documentation

转载 作者:行者123 更新时间:2023-12-01 00:37:46 25 4
gpt4 key购买 nike

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html

使用文档中提供的代码,我尝试迭代parent_dir,如果有zip 文件,那么我想将其复制到我的S3 存储桶。

我都尝试过

    try:
response = s3_client.upload_file(file_name, bucket, object_name)
except ClientError as e:
logging.error(e)
return False
return True

s3 = boto3.client('s3')
with open("FILE_NAME", "rb") as f:
s3.upload_fileobj(f, "BUCKET_NAME", "OBJECT_NAME")

但是他们都给出了同样的错误。

    s3_client = boto3.client(
's3',
aws_access_key_id='MY_KEY_ID',
aws_secret_access_key='MY_ACCESS_KEY'
)

session = boto3.Session(
aws_access_key_id='MY_KEY_ID',
aws_secret_access_key='MY_ACCESS_KEY',
)

s3 = session.resource('s3')
bucket = s3.Bucket('MY_URL')
    for file in os.listdir(parent_dir):
if object_name is None:
object_name = file

if file.endswith('.zip'):
with open(file, "rb") as f:
s3_client.upload_fileobj(f, bucket, object_name)

类型错误:预期的字符串或类似字节的对象

最佳答案

根据[AmazonAWS.Boto3]: S3.Client - upload_fileobj(Fileobj, Bucket, Key, ExtraArgs=None, Callback=None, Config=None) ,第二个nd和第三个rd参数(BucketKey)必须是字符串。

但是您将作为第二个nd参数传递:

bucket = s3.Bucket('MY_URL')

不行。使其成为纯字符串(更好的是,重命名它):

bucket_name = "MY_URL"

并将其传递给upload_fileobj,您应该可以解决这个问题。

关于python - 类型错误 : expected string or bytes-like object - how to copy files to an S3 bucket using the code provided in AWS's documentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57614903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com