簡體   English   中英

如何將文件上傳到 s3(使用 Python),其中 s3 存儲桶名稱的名稱中有斜杠“/”

[英]How to upload a file into s3 (using Python) where the s3 bucket name has a slash '/' in the name

我有一個 s3 桶(水桶/樣本),我正在嘗試將一個 .json 文件寫入其中。 但是,s3 存儲桶的名稱中有一個斜杠 (/),當我嘗試將文件移動到該位置(水桶/樣本)時,會導致返回錯誤:

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):[az\-0-9]*:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\-.]{1,63}$|^arn:(aws).*:s3-outposts:[az\-0-9]+:[0-9]{12}:outpost[/:][a-zA-Z0-9\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\-]{1,63}$"

我成功地將 my.json 文件寫入“waterbucket”s3 存儲桶。 我已經四處搜索並嘗試使用前綴(以及其他一些東西)但沒有運氣。 有沒有辦法讓我的 python 腳本寫入“水桶/樣本”桶而不是“水桶”?

下面是我的 python 腳本:

import boto3
import os
import pathLib
import logging

s3 = boto3.resource("s3")
s3_client = boto3.client(service_name = 's3')
bucket_name = "waterbucket/sample"
object_name = "file.json"

file_name = os.path.join(pathlib.Path(__file__).parent.resolve(), object_name) s3.meta.client.upload_file(file_name, folder.format(bucket_name), object_name)

提前致謝!

存儲桶名稱不能有斜線。 因此,在您的情況下, sample必須是對象名稱的一部分,因為它將被視為s3 前綴

bucket_name = "waterbucket"
object_name = "sample/file.json"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM