分片上传接口 融合接口 功能说明 分片上传步骤较多,包括初始化、文件切片、各个分片上传、完成上传。为了简化分片上传,可以使用AWS.S3.ManagedUpload接口进行分片上传。 代码示例 plaintext let key "ExampleObject.txt" let localFile "E:/ExampleObject.txt" let f fs.createReadStream(localFile) let upload new AWS.S3.ManagedUpload({ service: this.s3Client, partSize: 10 1024 1024, // 10M一片,可以根据需要自己定义,每个文件不能超过10000分片 params: { Bucket: this.bucket, Key: key, Body: f, ACL: "private", // 初始化acl权限,默认为private,"private""publicread""publicreadwrite" ContentType: "text/plain", // 设置contentType, 默认是application/octetstream }, }); upload.on("httpUploadProgress", progress > console.log(progress)) upload.send((err, data) > { if (err) { console.log("Error", err); } else { console.log("Success", data); } }); 关于ContentType的配置 ContentType用于标识文件的资源类型,比如image/png, image/jpg 是图片类型,video/mpeg, video/mp4是视频类型,text/plain, text/html是文本类型, 浏览器针对不同的ContentType会有不同的操作,比如图片类型可以预览,视频类型可以播放,文本类型可以直接打开。application/octetstream类型会直接打开下载窗口。 有些用户反馈图片和视频无法预览的问题,主要就是ContentType没有正确设置导致的;ContentType参数需要用户主动设置,默认是application/octetstream。在nodejs中,可以根据对象key值后缀扩展名来决定文件的ContentType,参考代码如下: plaintext let mime require("mimetypes") let mimeType (key) > { let ret mime.lookup(key); if (ret false) { return ""; } return ret; }