我正面臨一個問題,我正在嘗試測驗我的身份驗證以將檔案上傳到 S3 存盤桶中。目前我遇到了拒絕訪問的問題,但我想確保我的配置按預期作業。我理解這段代碼的作業方式是我使用的是組態檔配置“PROFILE_CONFIG_1”。這意味著我正在使用此配置捕獲 access_keys 和密鑰。
然后,我只是將我的本地檔案放入 S3 容器中。我懷疑我是否能夠使用 S3 瀏覽器按預期將檔案實際放置在路徑中,但代碼告訴我的方式不同。
def upload_to_aws():
session = boto3.Session(profile_name='PROFILE_CONFIG_1')
dev_s3_client = session.client('s3')
local_file = 'test.txt'
bucket = 'myBucketName'
filename = 'path/to/dir/'
with open(local_file, 'rb') as f:
dev_s3_client.upload_fileobj(f, bucket, filename) # Fails with error (AccessDenied) when calling the PutObject operation
更新:我嘗試使用 AWS CLI 添加一個檔案,一切都按預期作業。直到現在我才在 python 代碼中看到拒絕訪問問題。我也通過代碼簡化為這樣的東西,但仍然看到錯誤訊息。
session = boto3.Session(profile_name='PROFILE_CONFIG_1')
s3_client = session.client('s3')
s3_client.upload_file('Path_To_File\\test.txt', 'myBucketName','test.txt')
約翰的更新 2:
s3 = boto3.client('s3')
with open("test_file.txt", "rb") as f:
s3.upload_fileobj(f, 'take-uat-ics', 'destination_path/test_file.txt')
uj5u.com熱心網友回復:
您發布的代碼中存在兩個與目標 S3 物件鍵相關的問題。
第一個是使用path/to/dir/目標 S3 物件鍵。S3 副本不像常規的 Windows/Mac/Linux 檔案副本那樣作業,您可以使用cp file.txt /usr/mary/與/usr/mary/file.txt. 這不適用于 S3 API。目標必須是 S3 物件的完整密鑰。
第二個問題是在/destination_path/test_file.txt. S3 密鑰不需要或不允許將前導正斜杠作為 S3 存盤桶根的指示符。
有效的 S3 物件鍵既不以正斜杠開頭也不以斜杠結尾。有效 S3 物件鍵的示例如下:
dogs/small/poodle.png
destination_path/test_file.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/416578.html
標籤:
上一篇:適當地回購S3minio/aws
下一篇:AWSAthena查詢回傳空字串
