我正在運行一個 Python Lambda 函式,該函式使用請求庫從服務器下載 MP3 檔案,然后將檔案上傳到 S3。這是我正在使用的代碼,它作業正常:
dl_url = "https://server.com/file.mp3"
response = requests.get(dl_url)
s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
file_name = "file.mp3"
dir_file = f"/tmp/{file_name}"
with open(dir_file, "wb") as f:
f.write(response.content)
bucket.upload_file(dir_file, file_name)
此代碼作業正常,但是我想知道是否可以跳過先保存檔案然后上傳檔案的步驟。
uj5u.com熱心網友回復:
不需要您先保存檔案的作業代碼:
import boto3
import requests
s3_client = boto3.client('s3')
bucket = "test-bucket"
dl_url = "https://file-examples.com/storage/fe1170c1cf625dc95987de5/2017/11/file_example_MP3_700KB.mp3"
response = requests.get(dl_url)
# Write the object
s3_client.put_object(Bucket=bucket,
Key="sample_mp3.mp3",
Body=response.content)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/463944.html
