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)
TypeError: expected string or bytes-like object