【发布时间】:2021-04-23 03:30:20
【问题描述】:
我想创建一组文件夹,我想在其中将我的文件上传到 s3 存储桶中。 但是,我没有得到所需的文件名。 这是我的代码
s3 = boto3.resource('s3')
def upload_to_aws(local_file, bucket, s3_file):
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
try:
s3.upload_file(local_file, bucket, s3_file)
print("Upload Successful")
return True
except FileNotFoundError:
print("The file was not found")
return False
except NoCredentialsError:
print("Credentials not available")
return False
customer_name = "demo"
date = datetime.now().strftime('%d')
month = datetime.now().month
year = datetime.now().year
stack_path = "/home/ubuntu/pano/stack"
for file in sorted(os.listdir(stack_path)):
print(f"Uploading {file}")
folders_path = f"{customer_name}/{year}/{month}/{date}"
uploaded = upload_to_aws(f'/home/ubuntu/pano/stack/{file}', 'bucket-name1', '%s/%s' % (folders_path, file))
我希望文件夹是客户名称/年/月/日,我希望在其中上传文件。但是我得到的文件夹是 “顾客姓名/ ” “年/” “月/” “日期/” 我不想要文件夹名称中的反斜杠。我该怎么做?
编辑
我想要的文件夹具有以下路径 - 年/月/日/文件名.txt 但是,正在创建的路径具有以下文件夹名称 年//月//日//文件名.txt
每个文件夹名称都附有一个反斜杠。这些是我在我的存储桶中看到的目录
- 年/
- 月/
- 日期/
我想避免使用反斜杠和名称
【问题讨论】:
-
S3 中没有称为“文件夹”的概念。这些是帮助对文件进行分组的前缀。因此,您可以使用
my/awesome/file/content.json上传文件,而无需明确创建“文件夹”。 -
在 S3 中,您使用 Key 存储对象。理论上它没有文件夹。您能否举一个文件名为“abc.txt”的示例并解释您的预期和正在发生的事情?
-
以上所有的cmets都是真的,但我还是不明白你的问题是什么。你能详细解释一下吗?
-
@Azize 我已经在编辑中添加了详细信息
标签: python-3.x amazon-web-services amazon-s3 boto3