Menstrim Muat Naik Fail ke AWS S3 dengan Memori Minimum dan Jejak Cakera Fail
Masalah: Anda perlu memuat naik fail berbilang bahagian/data bentuk yang besar terus ke AWS S3 sambil meminimumkan memori dan cakera fail penggunaan.
Penyelesaian: Gunakan Pemuat Naik AWS S3 dengan langkah berikut:
Kod Contoh:
import ( "fmt" "os" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3/s3manager" ) // Configure Amazon S3 credentials and connection var accessKey = "" var accessSecret = "" func main() { // Determine if your AWS credentials are configured globally var awsConfig *aws.Config if accessKey == "" || accessSecret == "" { // No credentials provided, load the default credentials awsConfig = &aws.Config{ Region: aws.String("us-west-2"), } } else { // Static credentials provided awsConfig = &aws.Config{ Region: aws.String("us-west-2"), Credentials: credentials.NewStaticCredentials(accessKey, accessSecret, ""), } } // Create an AWS session with the configured credentials sess := session.Must(session.NewSession(awsConfig)) // Create an uploader with customized configuration uploader := s3manager.NewUploader(sess, func(u *s3manager.Uploader) { u.PartSize = 5 * 1024 * 1024 // Set the part size to 5MB u.Concurrency = 2 // Set the concurrency to 2 }) // Open the file to be uploaded f, err := os.Open("file_name.zip") if err != nil { fmt.Printf("Failed to open file: %v", err) return } defer f.Close() // Upload the file to S3 result, err := uploader.Upload(&s3manager.UploadInput{ Bucket: aws.String("myBucket"), Key: aws.String("file_name.zip"), Body: f, }) if err != nil { fmt.Printf("Failed to upload file: %v", err) return } fmt.Printf("File uploaded to: %s", result.Location) }
Dengan menggunakan Pemuat Naik S3 dan menstrim fail, anda boleh meminimumkan memori dan penggunaan cakera fail semasa proses muat naik, memastikan pengendalian fail besar yang cekap.
Atas ialah kandungan terperinci Bagaimana untuk Menstrim Fail Besar ke AWS S3 dengan Memori Minimum dan Penggunaan Cakera?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!