我正在使用Amazon S3服務,通過我的react native應用程式上傳圖片。 我使用了亞馬遜發送的簽名網址來進行上傳,所以它是這樣作業的:
。
- 當用戶打開相機拍攝照片時,它會向我的服務器發送一個獲取請求,以獲得url鏈接。
- 然后,當用戶驗證了圖片后,我使用該簽名的url來進行請求。
這里的問題是,檔案(jpeg圖片)在亞馬遜S3中上傳后被破壞。因此,上傳作業正常,但我無法打開圖片。我嘗試使用 Insomnia,它可以很好地打開圖片,因此它必須與我通過 react-native 應用程式發送的 formData 體有關。
以下是我的組件在 react native 中的代碼:
const result = await ImagePicker. launchCameraAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images。
allowsEditing: true。
aspect: [1, 1] 。
});
if (!result.cancelled) {
const resizeResult = await ImageManipulator.manipulateAsync(
result.uri。
[{ resize: { height: 400, width: 400 } }],
{ format: "jpeg", compress: 0.8 }
);
const formData = new FormData();
formData.append("putObject"/span>, {
uri: resizeResult.uri,
name: `${infoUser.id}`, //這里的名字應該不重要,因為它已經在查詢中定義了(但我使用的是已經被簽名的那個).
type: "image/jpeg"。
});
const config = {
headers: {
"Content-Type": "image/jpeg", // I also tried : "multipart/form-data" .
},
};
// results.data是簽名的url。
axios.put(results.data, formData, config)。
.then((res) => console.log(res)
.catch((e) => console。 log(e.response) )。
簽名的url看起來像這樣:
https://bucketname.s3.eu-west-3.mazonaws.com/profile.jpg? Content-Type=image/jpeg&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIASUE*******7/eu-west-3/s3/aws4_request&X-Amz-Date=20210917T094406Z& X-Amz-Expires=900&X-Amz-Signature=a6828c1669*********ffe641b4a&X-Amz-SignedHeaders=host;x-amz-acl&x-amz-acl=public-read
在使用亞馬遜s3之前,我使用完全相同的formData物件向我的服務器發送圖片,而且作業得很好。
謝謝你
uj5u.com熱心網友回復:
經過多次試驗,我成功地將我的圖片上傳到Amazon S3,但我真的不知道那是如何作業的。
所以我沒有使用
const formData = new FormData();
我只是將物件resizeResult作為我的主體請求傳遞。
請求看起來是這樣的:
await fetch(results.data, {
method: "PUT"。
body: resizeResult,
})
.then(() => console.log("success?")
.catch((err) => {
console.log(err)。
});
其中resizeResult是
Object {
"height"。2112,
"uri": "file://data/user/0/host.exp.exponent/cache/ExperienceData/%40yoann84%2FoneTribe/ImageManipulator/95843dca-e89a-4cec-977b-cd2177d71f57.jpeg"/span>。
"width": 400,
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/330521.html
標籤:
下一篇:在任何遞回函式中獲得無用的變數?
