balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

How to set Object name with "Bucket" ? #4828

Closed nidhi95 closed 5 years ago

nidhi95 commented 8 years ago

I would like to store data in "bucket_name/folder_name/file"

vikeen commented 8 years ago

:+1: for this issue. I am currently having a problem with trying to upload to a folder under a S3 bucket.

oeroche commented 8 years ago

Hi,

you can use the saveAs option:

req.file('file')
.upload({
 adapter: require('skipper-s3'),
 key: 'key',
 secret: 'secret',
 bucket: 'bucket_name',
 saveAs: 'folder_name/file'
})
vikeen commented 8 years ago

Ok, wow. I'm not sure how I missed that. I ended up actually neededing dirname. I wanted to place some audio files under an audio folder.

thank you

vikeen commented 8 years ago

Although saveAs works. I'm not sure how to leverage it the way I need?

req.file('song').upload({
  adapter: skipperS3,
  key: sails.config.aws.s3.accessKeyId,
  secret: sails.config.aws.s3.secretAccessKey,
  saveAs: 'audio/test.png',
  bucket: 'music-tribe'
}, function (error, updoadedFiles) {
  if (error) {
    return res.serverError(error);
  }

  return res.redirect('/dashboard');
});

Here are my problems.

  1. I need to know the file name and extension if I'm going to build the uuid for the fil name myself.
  2. I tried to use the dirName property, but it didn't seem to work.
  3. Determining the file name would also let me determine the type of file encoding being upload; something I can leverage later.

Any thoughts on this?

A possible solution is here (http://stackoverflow.com/questions/23977005/uploading-files-using-skipper-with-sails-js-v0-10-how-to-retrieve-new-file-nam).

This seems like a large amount of work for what I think should already exist.

mikermcneil commented 8 years ago

@vikeen Thanks for posting your research! It'll be especially helpful for folks finding this issue via search.

This seems like a large amount of work for what I think should already exist. Agreed-- although I think specifying a function for saveAs would solve this problem, right? I think we need to expand the docs on the subject though, since I don't see anything about how to access the filename from the stream in the README.

So in the example you posted:

req.file('song').upload({
  adapter: skipperS3,
  key: sails.config.aws.s3.accessKeyId,
  secret: sails.config.aws.s3.secretAccessKey,
  saveAs: function (file) {
    var origFilename = file.filename;
    var origExtension = require('path').extname(file.filename);
    return 'whatever/transmographied/filename/you/want.png';
  },
  bucket: 'music-tribe'
}, function (error, uploadedFiles) {
  if (error) {
    return res.serverError(error);
  }

  return res.redirect('/dashboard');
});

I'd love to see a proposal with some thoughts on how we could improve the docs on this if you have time.

btw- speaking of docs, on a separate note, if anyone's got a few minutes, I think it would be a good idea to bring in documentation for Upstreams' .upload() method into sails-docs reference so it can be rendered as HTML (will make this all easier to read). Could convert http://sailsjs.org/documentation/reference/request-req/req-file into a folder and create upload as a markdown file inside.

Thanks!

vikeen commented 8 years ago

@mikermcneil I was able to solve the problem like this.

/**
 * FilesController
 * @description :: Server-side logic for files
 */

import fs from 'fs';
import _ from 'lodash';
import skipperS3 from 'skipper-s3';

export function upload(req, res, next) {
  var originalFileName = req.file('file')._files[0].stream.filename,
    contentType = FileService.getContentTypeFromFileExtention(originalFileName);

  sails.log.verbose("attempting to upload file [%s] as file type [%s]", originalFileName, contentType);

  req.file('file').upload({
    adapter: skipperS3,
    key: sails.config.aws.s3.accessKeyId,
    secret: sails.config.aws.s3.secretAccessKey,
    dirname: contentType,
    bucket: 'music-tribe'
  }, function (error, updoadedFiles) {
    if (error) {
      return res.serverError(error);
    } else {
   // do stuff
  });
}

The problem was me calling dirName instead of dirname for the s3 upload config.

However, it was not clear to me that core features of the knox client were being inherited via skipper and then to skipper-s3 (e.g. saveAs and dirname). Maybe that's worth noting on the plugins?

sailsbot commented 8 years ago

Thanks for posting, @nidhi95. I'm a repo bot-- nice to meet you!

It has been 30 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message. On the other hand, if you are still waiting on a patch, please:

Thanks so much for your help!