![](/img/trans.png)
[英]downloading image file from AWS S3 bucket using nodejs, UPDATE :12/12
[英]How to upload an image file from an s3 bucket to cloudinary (nodejs)
我将图像文件保存在 s3 存储桶中。 我想使用 Cloudinary 更新图像。 将图像从 S3 取出并放入 Cloudinary 的最佳方法是什么?
我可以使用 nodeJS 中的 aws-sdk 获得可读流:
// Create service client module using ES6 syntax.
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
// Set the AWS Region.
const REGION = "eu-west-2";
// Create an Amazon S3 service client object.
const s3Client = new S3Client({ region: REGION });
// Set the parameters.
export const bucketParams = {
Bucket: "mybucketname",
};
// Get an object from S3 bucket
export async function getS3Object(inputParams: { Key: string }) {
try {
const data = await s3Client.send(
new GetObjectCommand({
...bucketParams,
Key: `public/dalle/${inputParams.Key}`,
})
);
return data; // data.Body is a readable stream
} catch (err) {
console.log("Error", err);
}
}
上传到 cloudinary 可以通过传递图像 url 来完成:
import { v2 } from "cloudinary";
const cloudinary = v2;
// Return "https" URLs by setting secure: true
cloudinary.config({
secure: true,
cloud_name: myCloudName,
api_key: myApiKey,
api_secret: myApiSecret,
});
export async function uploadImage(fileLocation: string){
const newUploadUrl = await cloudinary.uploader.upload(fileLocation, {});
console.log({
newUploadUrl,
});
}
Cloudinary 有没有办法接受可读流进行上传? 或者,有没有办法从 S3 获取公共图像 URL? (或者是否有更好的方法来完全做到这一点?)
我们可以创建一个临时 URL 发送到 cloudinary,而不是从 S3 下载图像:
// Create service client module using ES6 syntax.
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
// Set the AWS Region.
const REGION = "eu-west-2";
// Create an Amazon S3 service client object.
const s3Client = new S3Client({ region: REGION });
// Set the parameters.
export const bucketParams = {
Bucket: myBucketName,
};
export async function getTempSignedUrl(inputParams: { Key: string }) {
try {
const command = new GetObjectCommand({
...bucketParams,
Key: inputParams.Key,
});
const data = await getSignedUrl(s3Client, command, {});
console.log({
data,
});
return data; // Can be passed to cloudinary as per the upload function in the question
} catch (err) {
console.log("Error", err);
}
}
请确保可以公开访问 S3 URL 或为我们的 AWS 用户分配读取权限。 有关更多信息:http: //support.cloudinary.com/hc/en-us/articles/203276521-How-do-I-allow-Cloudinary-to-read-from-my-private-S3-bucket-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.