我有一个名为 task-details
的 AWS S3 存储桶和一个名为 archive
的文件夹,因此 S3 URI 为 s3://task-details/archive/
和 arn:aws:s3:::task-details/archive/
的 ARN。我正在尝试使用 Python 的 boto3 包中的 upload_file
方法将 CSV 文件上传到 S3 存储桶中的文件夹。
下面是我用来尝试将数据上传到文件夹的方法,但我不断收到正则表达式错误,这让我认为我无法将数据上传到存储桶中的特定文件夹。有谁知道如何做到这一点?
方法:
import logging
import boto3
from botocore.exceptions import ClientError
def upload_file(file_name, bucket, object_name=None):
"""Upload a file to an S3 bucket
:param file_name: File to upload
:param bucket: Bucket to upload to
:param object_name: S3 object name. If not specified then file_name is used
:return: True if file was uploaded, else False
"""
# If S3 object_name was not specified, use file_name
if object_name is None:
object_name = file_name
# Upload the file
s3_client = boto3.client('s3')
try:
response = s3_client.upload_file(file_name, bucket, object_name)
except ClientError as e:
logging.error(e)
return False
return True
我的代码(我也试过 bucket = s3://task-details/archive/
和 bucket = task-details/archive/
):
upload_file(
file_name = filepath,
bucket = "arn:aws:s3:::task-details/archive/",
object_name = object_name
)
错误:
Invalid bucket name "arn:aws:s3:::task-details/archive/": Bucket name must match the regex
"^[a-zA-Z0-9.\-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).*:(s3|s3-object-lambda):[a-z\-0-9]+:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\-]{1,63}$|^arn:(aws).*:s3-outposts:[a-z\-0-9]+:[0-9]{12}:outpost[/:][a-zA-Z0-9\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\-]{1,63}$"
最佳答案
API 调用需要存储桶名称或 ARN。您的存储桶名称是 task-details
,您的存储桶 ARN 是 arn:aws:s3:::task-details
。
您在调用 upload_file 时使用了 Key 参数指定对象的键,例如 archive/cats/persian.png
。请注意,S3 对象键不仅仅是对象/文件名,还包括前缀/文件夹。
https://stackoverflow.com/questions/67876666/
相关文章:
ibm-midrange - 带有预填充元素的 iSeries/IBM i 命令 (CMDSRC)
python - 是否有一个 Binance API 端点来关闭所有头寸?
python - 使用另一个 dataFrame 更改 Pandas dataFrame 中的列中的
r - ggplot : create a facet grid with free scales
python - 如何在 python 中反转嵌套列表中的字符串列表?
r - 如何从列中提取不同的值并对其求和并创建一个包含总和的列