簡體   English   中英

使用預簽名 URL 上傳后,AWS S3 上的文件在文件中包含額外信息

[英]File on AWS S3 having extra information in file after uploading using presigned URL

我正在嘗試使用預先簽名的 url 將文件直接上傳到 S3 存儲桶。

下面是用node.js編寫的服務器端代碼,它成功創建了簽名url。

const s3 = new AWS.S3({
    accessKeyId: config.accessKeyId,
    secretAccessKey:  config.secretAccessKey,
    region: config.awsConfig.region
});
const signedUrlExpireSeconds = 60 * 60;
let mimeType = "video/mp4";

const filename =Date.now();

const Key = `${filename}.mp4`;

const params = {
    Bucket: config.awsConfig.aws_upload_bucket, 
    Key: Key,
    Expires: signedUrlExpireSeconds,
    ACL: 'public-read',
    ContentType: mimeType
    };

s3.getSignedUrl('putObject', params, function (err, url) {
    if (err) {
        console.log('Error getting presigned url from AWS S3');
        res.json({ success: false, message: 'Pre-Signed URL error', urls: fileurls });
    }
    else {
        console.log('Presigned URL: ', url);
        res.json({ success: true, message: 'AWS SDK S3 Pre-signed urls generated successfully.', url: url, Key:Key, ContentType: mimeType  });
    }
});

下面是在 React 端編寫的代碼。

const DropzoneArea = props => {
    const [files, setFiles] = useState([]);
    let awsFile = '';

    const onUploadHandler = files => {
        if (files.length === 0) {
            console.log('Debug : [components DropzoneArea onUploadHandler] files => ', files);
            return;
        }
        awsFile = files[0]
        // calling the API to get presigned url.
        getPreSignedURL(files[0].type,S3preSignedURLCallback)        
    };

    const S3preSignedURLCallback = videoData => {
        const xhr = new XMLHttpRequest();
        xhr.open('PUT', videoData.url);
        xhr.setRequestHeader('Content-Type', videoData.ContentType);
        var res = new FormData();
        res.append('file', awsFile);
        xhr.send(res);
    };
}

使用上面的代碼文件成功上傳到 s3 但視頻無法播放。 所以我將 S3 上的文件與我上傳的實際文件進行了比較。 我發現在 S3 文件中也寫入了一些額外的請求數據。 在此處輸入圖片說明

我在這里做的一個小錯誤,無法定位錯誤。 請幫忙。

不要使用formData ,直接發送文件即可。

const S3preSignedURLCallback = videoData => {
        const xhr = new XMLHttpRequest();
        xhr.open('PUT', videoData.url);
        xhr.setRequestHeader('Content-Type', videoData.ContentType);
        // Send the binary data.
        // Since a File is a Blob, you can send it directly.
        xmlHttpRequest.send(awsFile);
};

您看到的不同之處在於 AWS 正在保存原始請求,而不對其進行解析,您可以看到正確的圖像是多部分/表單數據請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM