將大型文件直接上傳到AWS S3,同時最大限度地減少內存和磁碟佔用空間是一項重要任務在雲端運算中。本指南將示範如何使用適用於 Go 的 AWS 開發工具包來實現此目的。
要將檔案上傳直接串流到 S3,您可以使用 s3manager 套件。以下是逐步解決方案:
設定AWS 憑證和會話:
建立 S3 上傳器:
開啟檔案:
上傳檔案:
package main import ( "fmt" "os" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3/s3manager" ) const ( filename = "file_name.zip" myBucket = "myBucket" myKey = "file_name.zip" accessKey = "" accessSecret = "" ) func main() { var awsConfig *aws.Config if accessKey == "" || accessSecret == "" { //load default credentials awsConfig = &aws.Config{ Region: aws.String("us-west-2"), } } else { awsConfig = &aws.Config{ Region: aws.String("us-west-2"), Credentials: credentials.NewStaticCredentials(accessKey, accessSecret, ""), } } // The session the S3 Uploader will use sess := session.Must(session.NewSession(awsConfig)) // Create an uploader with the session and default options //uploader := s3manager.NewUploader(sess) // Create an uploader with the session and custom options uploader := s3manager.NewUploader(sess, func(u *s3manager.Uploader) { u.PartSize = 5 * 1024 * 1024 // The minimum/default allowed part size is 5MB u.Concurrency = 2 // default is 5 }) //open the file f, err := os.Open(filename) if err != nil { fmt.Printf("failed to open file %q, %v", filename, err) return } //defer f.Close() // Upload the file to S3. result, err := uploader.Upload(&s3manager.UploadInput{ Bucket: aws.String(myBucket), Key: aws.String(myKey), Body: f, }) //in case it fails to upload if err != nil { fmt.Printf("failed to upload file, %v", err) return } fmt.Printf("file uploaded to, %s\n", result.Location) }
範例程式碼
以下程式碼範例示範如何使用 s3manager 將大檔案上傳到 AWS S3:
package main 進口 ( “FMMT” "作業系統" “github.com/aws/aws-sdk-go/aws/credentials” “github.com/aws/aws-sdk-go/aws” “github.com/aws/aws-sdk-go/aws/session” “github.com/aws/aws-sdk-go/service/s3/s3manager” ) 常量( 檔案名稱=“檔案名稱.zip” myBucket = “我的Bucket” myKey =“檔案名稱.zip” 存取密鑰=“” 訪問密碼=“” ) 函數主() { var awsConfig *aws.Config 如果 accessKey == "" ||訪問秘密==“”{ //載入預設憑證 awsConfig = &aws.Config{ 區域:aws.String("us-west-2"), } } 別的 { awsConfig = &aws.Config{ 區域:aws.String("us-west-2"), 憑證:credentials.NewStaticCredentials(accessKey, accessSecret, ""), } } // S3 上傳器將使用的會話 sess := session.Must(session.NewSession(awsConfig)) // 使用會話和預設選項建立上傳器 //上傳者 := s3manager.NewUploader(sess) // 使用會話和自訂選項建立上傳器 上傳者 := s3manager.NewUploader(sess, func(u *s3manager.Uploader) { u.PartSize = 5 * 1024 * 1024 // 允許的最小/預設部分大小為 5MB u.Concurrency = 2 // 預設為 5 }) //開啟檔案 f, err := os.Open(檔名) 如果錯誤! = nil { fmt.Printf("無法開啟檔案%q,%v",檔案名,錯誤) 返回 } //延遲f.Close() // 將檔案上傳到S3。 結果, err := uploader.Upload(&s3manager.UploadInput{ 儲存桶:aws.String(myBucket), 鍵:aws.String(myKey), 身體:f, }) //如果上傳失敗 如果錯誤! = nil { fmt.Printf("上傳檔案失敗,%v",err) 返回 } fmt.Printf("檔案已上傳至,%s\n", result.Location) }透過執行這些步驟,您可以有效率地將大型多部分/表單資料檔案直接上傳到 AWS S3,同時佔用最少的記憶體。免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3