[英]not able to create folders in aws s3 using boto3
我想創建一組文件夾,我想在其中將我的文件上傳到 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))
我希望文件夾是客戶名稱/年/月/日,我希望在其中上傳文件。 但是我得到的文件夾是“customer_name/”“year/”“month/”“date/”我不希望文件夾名稱中出現反斜杠。 我該怎么做呢?
編輯
我想要的文件夾具有以下路徑 - 年/月/日期/filename.txt 但是正在創建的路徑具有以下文件夾名稱年//月//日期//filename.txt
每個文件夾名稱都附有一個反斜杠。 這些是我在存儲桶中看到的目錄
我想避免使用反斜杠和名稱
這只是表示什么是“文件”或什么是“文件夾”。
但請記住,沒有文件夾的概念,它只是一個 object 密鑰。
請參閱下面我正在使用您的代碼。 它成功將文件上傳到存儲桶。
Python 3.8.0 (default, Feb 25 2021, 22:10:10)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import boto3
>>> from datetime import datetime
>>>
>>> def upload_to_aws(local_file, bucket, s3_file):
... print("###", local_file, bucket, s3_file)
... s3 = boto3.client('s3')
...
... 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 = "/tmp/test"
>>> 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'/tmp/test/{file}', 'test-bucket', '%s/%s' % (folders_path, file))
...
Uploading file1
### /tmp/test/file1 test-bucket demo/2021/4/22/file1
Upload Successful
Uploading file2
### /tmp/test/file2 test-bucket demo/2021/4/22/file2
Upload Successful
Uploading file3
### /tmp/test/file3 test-bucket demo/2021/4/22/file3
Upload Successful
>>> exit()
現在讓我們看看如何獲取文件。 請注意,文件的密鑰正是您所期望的,因此不要在其上加倍/
。
Python 3.8.0 (default, Feb 25 2021, 22:10:10)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
>>> s3 = boto3.client('s3')
>>> s3.get_object(Bucket='test-bucket', Key='demo/2021/4/22/file1')
{'ResponseMetadata': {'RequestId': 'RE...', 'HostId': 'eMmV3p+...', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amz-id-2': 'eMm...', 'x-amz-request-id': 'RE..', 'date': 'Thu, 22 Apr 2021 13:45:33 GMT', 'last-modified': 'Thu, 22 Apr 2021 13:41:09 GMT', 'etag': '"355..."', 'accept-ranges': 'bytes', 'content-type': 'binary/octet-stream', 'content-length': '11', 'server': 'AmazonS3'}, 'RetryAttempts': 0}, 'AcceptRanges': 'bytes', 'LastModified': datetime.datetime(2021, 4, 22, 13, 41, 9, tzinfo=tzutc()), 'ContentLength': 11, 'ETag': '"35..."', 'ContentType': 'binary/octet-stream', 'Metadata': {}, 'Body': <botocore.response.StreamingBody object at 0x7f2b2ea8ddf0>}
從 AWS CLI。 在第一個 output 上,您可以看到最后的/
,但這只是一個表示,請查看下面的完整 object 密鑰。
$ aws s3 ls s3://test-bucket/demo
PRE demo/
$ aws s3 ls s3://test-bucket/demo --recursive
2021-04-22 10:41:09 11 demo/2021/4/22/file1
2021-04-22 10:41:09 11 demo/2021/4/22/file2
2021-04-22 10:41:10 11 demo/2021/4/22/file3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.