简体   繁体   English

TypeError:预期的字符串或类似字节的对象 - 如何使用 AWS 文档中提供的代码将文件复制到 S3 存储桶

[英]TypeError: expected string or bytes-like object - how to copy files to an S3 bucket using the code provided in AWS's documentation

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

Using the code provided in the documentation, I'm trying to iterate through parent_dir, and if there's a zip file, then I want to copy it to my S3 bucket.使用文档中提供的代码,我尝试遍历 parent_dir,如果有 zip 文件,那么我想将其复制到我的 S3 存储桶中。

I tried both我都试过了

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

and

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

but both of them gave the same error.但他们都给出了同样的错误。

    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)

TypeError: expected string or bytes-like object

According to [AmazonAWS.Boto3]: S3.Client - upload_fileobj( Fileobj, Bucket, Key, ExtraArgs=None, Callback=None, Config=None ) , the 2 nd and 3 rd arguments ( Bucket and Key ) must be strings.根据[AmazonAWS.Boto3]: S3.Client - upload_fileobj( Fileobj, Bucket, Key, ExtraArgs=None, Callback=None, Config=None )第二个第三个参数( BucketKey )必须是字符串。

But you are passing as a 2 nd argument:但是你是作为第二个参数传递的:

bucket = s3.Bucket('MY_URL')

which is not OK .这也不 Make it a plain string (and even better, rename it):使它成为一个普通的字符串(甚至更好,重命名它):

bucket_name = "MY_URL"

and pass it to upload_fileobj , and you should get past this problem.并将其传递给upload_fileobj ,您应该可以解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS S3 写入 object - TypeError: expected string or bytes-like object - AWS S3 write an object - TypeError: expected string or bytes-like object 从 s3 存储桶下载数据时出现预期的字符串或类似字节的 object 错误 - expected string or bytes-like object error while downloading data from s3 bucket 亚马逊 s3 文件下载“类型错误:预期的字符串或类似字节的对象” - Amazon s3 file download "TypeError: expected string or bytes-like object" 尝试使用Boto3从S3下载特定类型的文件-TypeError:预期的字符串或类似字节的对象 - Trying to download specific type of file from S3 using Boto3 - TypeError: expected string or bytes-like object Python Boto3 错误 - 删除 Amazon S3 对象时出现“预期的字符串或类似字节的对象” - Python Boto3 error - "expected string or bytes-like object" when deleting an Amazon S3 object 类型错误:预期的字符串或类似字节的对象; - TypeError: expected string or bytes-like object; TypeError:期望的字符串或类字节对象 - TypeError: expected string or bytes-like object TypeError:预期的字符串或类似字节的对象1 - TypeError: expected string or bytes-like object 1 TypeError:预期的字符串或字节对象 - TypeError: string or bytes-like object expected TypeError:预期的字符串或类似字节的对象' - TypeError: expected string or bytes-like object'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM