我想将我的 mp3 文件上传到 S3,获取资源的 URL,然后将其发送给用户。
由于上传可能需要 10 秒以上,因此我想对上传进行流式传输并允许用户从我的存储桶中进行流式传输。但是,在整个文件上传完成之前,该 URL 似乎无效。
const passThroughStream = new stream.PassThrough();
myAudioStream.pipe(passThroughStream);
const uploadParams = {
Bucket: bucketName,
Key: key,
Body: readable,
};
s3.upload(uploadParams, function (err, data) {
console.log("full mp3 uploaded");
});
// Wait 500ms for first few bytes to upload before streaming the file
await new Promise((resolve) => setTimeout(resolve, 500));
const fileUrl = s3.getSignedUrl("getObject", {
Bucket: bucketName,
Key: key,
Expires: 120,
});
console.log("File URL", fileUrl);
如果我点击此 URL,我会看到以下内容。
在 10 秒结束之前我无法访问该资源。我以为我一开始上传就能访问它。
这是正确的。 Amazon S3 中的对象要么“全部存在”,要么“根本不存在”。 无法访问仍在上传的对象。
如果您希望“流式传输”对象,那么您可以考虑使用专门流式传输的服务,例如 Amazon Kinesis。