دروووود به همه
من هنگام آپلود فایل های حجیم +400mb چون باید مولتی پارت بشه به مشکل خوردم و اینطوری حلش کردم :)
کد توی داکیومنت آروان :
const file = __dirname + '/../files/file.png'; //FILE_NAME (the name of the file to upload (if you don't specify KEY))
const fileStream = fs.createReadStream(file);
fileStream.on('error', function (err) {
console.log('File Error', err);
});
const uploadParams = {
Bucket: 'sample_bucket', // bucket name
Key: 'KEY', // the name of the selected file
};
uploadParams.Key = path.basename(file);
const createUploadResponse = await s3.send(
new CreateMultipartUploadCommand(uploadParams)
);
const { Bucket, Key } = uploadParams;
const { UploadId } = createUploadResponse;
console.log('Upload initiated. Upload ID: ', UploadId);
// 5MB is the minimum part size
// Last part can be any size (no min.)
// Single part is treated as last part (no min.)
// const partSize = (1024 * 1024) * 5 // 5MB
const partSize = 1024 * 5; // 5MB
const fileSize = file.length;
const numParts = Math.ceil(fileSize / partSize);
متغیر file.length طول ادرس فایل رو بر میگردونه ولی ما دنبال حجم فایلیم به جای کد بالا میشه از کد زیر استفاده کرد
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
const { Upload } = require("@aws-sdk/lib-storage");
const fs = require("fs");
const path = require("path")
const partSize = 1024 * 1024 * 300; // 300MB
const s3 = new S3Client({
region: "default",
endpoint: ENDPOINT,
credentials: {
accessKeyId: ACCESS_KEY_ID,
secretAccessKey: SECRET_ACCESS_KEY,
},
});
async function multiPartUpload(file) {
try {
let time_started = new Date();
let time_elapsed;
let upload_speed;
let seconds_left;
const fileStream = fs.createReadStream(file);
uploadParams.Key = path.basename(file);
// call S3 to upload file to specified bucket
uploadParams.Body = fileStream;
const upload = new Upload({
client: s3,
queueSize: 4, // optional concurrency configuration
partSize, // optional size of each part
leavePartsOnError: false, // optional manually handle dropped parts
params: uploadParams,
});
upload.on("httpUploadProgress", (progress) => {
time_elapsed = new Date() - time_started;
upload_speed = progress.loaded / (time_elapsed / 1000);
seconds_left = Math.round(
(progress.total - progress.loaded) / upload_speed
);
console.log(
"Progress: " +
Math.round((progress.loaded / progress.total) * 100) +
"%"
);
});
const result = await upload.done();
console.log(result);
} catch (e) {
console.log(e);
}
}