問題是
我有一個檔案,我想把它寫到我的S3桶中的一個特定檔案夾中,讓我們把桶稱為bucket,檔案夾稱為folderInBucket
我正在使用boto3庫來實作,并有以下功能:
我所使用的是boto3庫。
我做了什么 我也試過用 理解上的差距 這個函式或多或少是從 boto3 檔案中提取的。檔案并沒有真正說明如何將檔案寫入我們的S3桶中的特定檔案夾。我知道檔案本身能夠很好地寫入桶的主目錄,因為上面的代碼沒有問題。
uj5u.com熱心網友回復: 我通過修改你的函式和呼叫,將object_name作為bucket上的檔案的絕對路徑,使其能夠作業。
如果你仍然遇到錯誤,請分享錯誤。就檔案上傳而言,它應該與你已經做的事情一起作業。
uj5u.com熱心網友回復: S3是 "blob存盤",因此 "檔案夾 "的概念并不真正存在。 當上傳檔案時,你只需提供目標字串的完整前綴。
這里有一個例子: uj5u.com熱心網友回復: 沒有這樣的東西 - "檔案夾 "是s3. URL的例子。s3://mybucket/folder1/folder2/file.txt 在上面的例子中,檔案名是'folder1/folder2/file.txt' uj5u.com熱心網友回復: 桶不是檔案夾,但它們的作用類似于檔案夾。你在s3桶中存盤物件。一個物件有一個鍵。 默認情況下,檔案是以私人權限上傳的。你可以將acl設定為public-read,讓世界上的每個人都能看到這個檔案。
標籤:def upload_file(file_name。bucket, object_name = None)。)
if object_name is None:
object_name = file_name
s3 = b3.client('s3')
try:
response = s3.upload_file(file_name, bucket, Key=' bulk_movies.json')
except ClientError as e:
print(e)
return False[/span
print('Success! ')
return True
upload_file('./s3/bulk_movies.json'/span>, 'bucket'/span>)
bucket/folderInBucket作為第二個引數來呼叫這個函式,但這在代碼中產生了一個錯誤(實際上有點像預期的那樣)
import boto3 as b3
def upload_file(file_name, bucket, object_name)。
if object_name is None。
object_name = file_name
s3 = b3.client('s3')
try:
response = s3.upload_file(file_name, bucket, Key=object_name)
except ClientError as e:
print(e)
return False[/span
print('Success! ')
return True
upload_file('bulk_movies.json', '<bucket-name> ', 'folderInBucket/bulk_movies.json')
import boto3
s3 = boto3.resource('s3')
folder_name = 'folder_name''helloworld.txt', 'bucketname', folder_name '/helloworld.txt')
。
你認為 "檔案夾 "是檔案名的一部分。s3.upload_file(
Bucket='gid-requests'。
Filename=' bulk_movies.json',
Key='folderInBucket/bulk_movies.json',
ExtraArgs={'ACL':'public-read'})
