我有一個函式應該從外部 URL 獲取影像,使用sharp 對其進行優化,然后將影像上傳到 S3。
但似乎我無法敏銳地調整大小和壓縮。
這是我當前的代碼:
const got = require('got');
const sharp = require('sharp');
const AWS = require('aws-sdk');
const s3 = new AWS.S3({
credentials,
});
async function uploadFile(url) {
// Get file
const response = await got(url, { responseType: 'buffer' });
// Resize, compress and convert image before upload
const { data, info } = await sharp(response.body)
.resize({ width: 512 })
.jpeg({ quality: 70 })
.toBuffer();
console.log('data:', data);
const uploadedFile = await s3
.upload({
Bucket: bucket,
Key: 'filename.jpg',
Body: data,
ContentType: 'image/jpeg',
})
.promise();
console.log('uploadedFile:', uploadedFile);
}
我得到的錯誤: Input file is missing
sharp呼叫該方法時代碼失敗。
我從 response.body鏈接獲得的輸出型別
uj5u.com熱心網友回復:
我認為你需要做兩個改變,如下:
const body = await got(url).buffer();
和:
const data = await sharp(body)
.resize({ width: 512 })
.jpeg({ quality: 70 })
.toBuffer();
通過這些更改,您的代碼對我有用。我從網上隨機下載了一張圖片并成功上傳到 S3。另外,請確保您擁有最新的 got 和 sharp 軟體包。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/333200.html
標籤:javascript 节点.js 亚马逊网络服务 亚马逊-s3 锋利的
下一篇:當我們將資料從加密的AWSS3存盤桶發送到加密的GoogleCloudStorage存盤桶時,該資料在傳輸程序中是否加密?
