简体   繁体   English

如何将文件上传到AWS中的预签名URL?

[英]How do I upload a file to a pre-signed URL in AWS?

Problem 问题

I have a pre-signed URL from AWS. 我有一个来自AWS的预签名URL。 I need to upload a file to it. 我需要上传文件。 How do I do this? 我该怎么做呢?

(I have to use a pre-signed URL. I'm calling the createUpload API for Device Farm , which returns one.) (我必须使用预签名的URL。我正在调用Device FarmcreateUpload API ,该API返回一个。)

AWS Documentation gives examples on how to do this in Java, .NET and Ruby . AWS文档提供了有关如何在Java,.NET和Ruby中执行此操作的示例 JavaScript is missing. 缺少JavaScript。

Attempt 1 尝试1

I adapted the example here . 我在这里改了个例子。

const axios = require('axios');
const FormData = require('form-data');

function uploadFile(url, file) {
    if (typeof url !== 'string') {
        throw new TypeError(`Expected a string, got ${typeof url}`);
    }
    const formData = new FormData();
    formData.append(file,file)
    const config = {
        headers: {
            'content-type': 'multipart/form-data'
        }
    }
    return  axios.post(url, formData,config)
}

However I get this error: 但是我得到这个错误:

The request signature we calculated does not match the signature you provided. 我们计算出的请求签名与您提供的签名不匹配。 Check your key and signing method. 检查您的密钥和签名方法。

Attempt 2 尝试2

I got it working with cURL. 我可以使用cURL。 However, I don't want a dependency on cURL. 但是,我不想依赖cURL。

const Promise = require('bluebird');
const cmd = require('node-cmd');
const getAsync = Promise.promisify(cmd.get, { multiArgs: true, context: cmd }); 

async function uploadFile(url, fileName) { 
    await throwIfCurlNotInstalled();
    console.log('uploadFile: Uploading file', {
        fileName
    });
    const command = `curl -T ${fileName} "${url}"`;
    try {
        let result = await getAsync(command);
        result.forEach(line => console.log(line));
        console.log('uploadFile: File uploaded', {
            fileName,
        });
    } catch (e) {
        console.error('uploadFile: Error uploading file', {
            fileName
        });
        console.error(e);
    }
}
async function throwIfCurlNotInstalled() {
    try {
        await getAsync(`curl -V`);
    } catch (e) {
        throw 'curl is not installed';
    }
}

The solution was surprisingly simple: 解决方案非常简单:

const rp = require('request-promise');
const fs = require('fs');

async function uploadFile(url, fileName) {
    let options = {
        method: 'PUT',
        uri: url,
        body: fs.readFileSync(fileName),
    };

    return rp(options);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用预先签名的POST URL将文件上载到AWS S3时设置随机文件名 - Set random file name when upload file to AWS S3 with pre-signed POST url Dropzone 客户端通过文件上传到 AWS 预签名 url 调整大小 - Dropzone client side resize with file upload to AWS pre-signed url 如何使用预签名的 URL 而不是凭证直接从浏览器上传到 AWS S3? - How to upload to AWS S3 directly from browser using a pre-signed URL instead of credentials? 如何使用 angular 或 javascript 中的预签名 url 将文件上传到 S3 存储桶 - how to upload file to S3 bucket using pre-signed url in angular or javascript 如何使用后端预签名URL使用其SDK将文件上传到Amazon S3? - How can i use a backend pre-signed url to upload files to Amazon S3 using its SDK? S3使用预先签名的URL从浏览器上传图片 - S3 Upload image with pre-signed url from browser 将AWS javascript sdk putObject与预签名URL一起使用 - Using aws javascript sdk putObject with a pre-signed URL 使用angular + aws-sdk-js +预签名网址将文件上传到S3 - Uploading a file to S3 with angular + aws-sdk-js + pre-signed url 通过预签名URL将PUT上传到S3时拒绝签名 - Signature Denied in PUT upload to S3 with Pre-Signed URL Sketch JS,如何在音频标签上使用AWS预签名URL? - Sketch JS, how to use AWS pre-signed URL on audio tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM