Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

amazon s3 - Image upload to S3 with multer in Meteor

I'm currently trying to upload an image to a S3 bucket (digitalocean spaces). I found some information on using multer and multer s3 on the digitalocean spaces page, but they used express js to use a post request to send the file.

I tried to use a meteor method to send an image to the server and use multer to store it in s3.

The following code is used on the client side to capture the image and to send it to the server side:

'click #takePhoto': (event, template) => {
        canvas.width = video.videoWidth;
        canvas.height = video.videoHeight;
        canvas.getContext('2d').drawImage(video, 0, 0);
        const data = canvas.toDataURL('image/jpeg');
        document.getElementById('photo').setAttribute('src', data);
        // Meteor call to send the canvas to the server for storing on s3
        Meteor.call('uploadPicture', data, template.data);
}

On the server side I'm using the following code to connect to the s3 and where I tried to upload the file to mul

const spacesEndpoint = new AWS.Endpoint('ams3.digitaloceanspaces.com');
const s3 = new AWS.S3({
    endpoint: spacesEndpoint,
    accessKeyId: Meteor.settings.private.digitalOceanSpaces.accessKeyId,
    secretAccessKey: Meteor.settings.private.digitalOceanSpaces.secretAccessKey
});

var upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: 'test-bucket'
        metadata: function (req, file, cb) {
            cb(null, { fieldName: file.fieldname });
        },
        key: function (req, file, cb) {
            cb(null, Date.now().toString())
        }
    })
})

upload.single(picture);

Where picture at the final upload is from the canvas.toDataURL('image/jpeg') function that gets send to the server.

When I now run this function it doesn't give me an error but it also doesn't appear on the spaces, so it's not sending it correctly.

I would like to hear if someone was able to get this working or if they have used different solutions for uploading images to a s3 storage. I also couldn't figure out where I could catch an error somewhere here to get some more debugging info where it is giving me an issue.

Thank you in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...