[英]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 ) ,第二个和第三个参数( Bucket和Key )必须是字符串。
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.