如何使用Javascript将文件上传到Digital Ocean Spaces

时间:2017-10-08 18:12:17

标签: javascript amazon-s3 digital-ocean

我希望使用Digital Oceans空间(它似乎与S3具有相同的API),并希望通过上传示例文件来尝试它。我遇到了很多困难。这是我到目前为止所做的事情

{'hi' : 'world'}

我要上传的文件hiworld.json的内容。我知道在发出此请求之前我需要创建一个aws v4签名。

var aws4 = require('aws4') var request = require('request')

var opts = {'json': true,'body': "{'hi':'world'}",host: '${myspace}.nyc3.digitaloceanspaces.com', path: '/hiworld.json'}

aws4.sign(opts, {accessKeyId: '${SECRET}', secretAccessKey: '${SECRET}'})

然后我发送请求

request.put(opts,function(error, response) {
    if(error) {
        console.log(error);
    }
    console.log(response.body);
});

但是,当我查看我的数字海洋空间时,我发现我的文件没有创建。我注意到,如果我将PUT更改为GET并尝试访问现有文件,我就没有问题。

这是我的标题看起来像

headers: { Host: '${myspace}.nyc3.digitaloceanspaces.com', 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Content-Length': 14, 'X-Amz-Date': '20171008T175325Z', Authorization: 'AWS4-HMAC-SHA256 Credential=${mykey}/20171008/us-east-1//aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date, Signature=475e691d4ddb81cca28eb0dcdc7c926359797d5e383e7bef70989656822accc0' }, method: 'POST' }

3 个答案:

答案 0 :(得分:2)

作为替代方案,使用 library(shiny) library(shinyjs) library(DT) library(dplyr) library(data.table) #I don't need all these packages just yet but I will use them as I carry on the project hopefully ui = fluidPage( fileInput("Rams","Upload complete list of Rams", accept=".csv"), #fileinput where a list of rams (male sheep) are loaded. I want to select a few rams from the list based on their unique combinations of birth year, tag number and flock number br(), DT::dataTableOutput("Rams1") ) server = function(input, output, session) { #server section of my app #This shows me the data output$Rams1 <- renderDataTable({ Rams2 <- input$Rams if (is.null(Rams2)) return(NULL) subset(read.csv(Rams2$datapath, header=T, sep=",")) }) } shinyApp(ui=ui, server=server)

aws-sdk

答案 1 :(得分:1)

var str = {
    'hi': 'world'
}

var c = JSON.stringify(str);

request(aws4.sign({
  'uri': 'https://${space}.nyc3.digitaloceanspaces.com/newworlds.json',
  'method': 'PUT',
  'path': '/newworlds.json',
  'headers': {
    "Cache-Control":"no-cache",
    "Content-Type":"application/x-www-form-urlencoded",
    "accept":"*/*",
    "host":"${space}.nyc3.digitaloceanspaces.com",
    "accept-encoding":"gzip, deflate",
    "content-length": c.length
  },
  body: c
},{accessKeyId: '${secret}', secretAccessKey: '${secret}'}),function(err,res){
    if(err) {
        console.log(err);
    } else {
        console.log(res);
    }
})

这给了我一个成功的PUT

答案 2 :(得分:0)

可以使用multer和aws sdk完成。它对我有用。

const aws = require('aws-sdk');
const multer = require('multer');
const express = require('express');
const multerS3 = require('multer-s3');
const app = express();

const spacesEndpoint = new aws.Endpoint('sgp1.digitaloceanspaces.com');
const spaces = new aws.S3({
endpoint: spacesEndpoint,
accessKeyId: 'your_access_key_from_API',
secretAccessKey: 'your_secret_key'
 });


const upload = multer({
storage: multerS3({
s3: spaces,
bucket: 'bucket-name',
acl: 'public-read',
key: function (request, file, cb) {
  console.log(file);
  cb(null, file.originalname);
   }
 })
 }).array('upload', 1);

现在,您也可以使用这样的API调用此

app.post('/upload', function (request, response, next) {
upload(request, response, function (error) {
if (error) {
  console.log(error);

}
console.log('File uploaded successfully.');

   });
  });

HTML看起来像这样

<form method="post" enctype="multipart/form-data" action="/upload">
<label for="file">Upload a file</label>
<input type="file" name="upload">
<input type="submit" class="button">
</form>