question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Send additional data to Companion to create a directory in S3

See original GitHub issue

Hi, I’m trying to upload files to S3 with a standalone server, everything is working file, just one customization needed. I want the files to be uploaded in a directory in a bucket. So if I send username/file-name.jpg as the file Key then the S3 will create a directory-like structure in the bucket, so every time a user uploads a file, it’ll be uploaded in his/her folder. I’m using this code to send the files to the server. .use(Uppy.AwsS3, { companionUrl: 'https://uploader.domain.com' })

I want to send additional data like: .use(Uppy.AwsS3, { companionUrl: 'https://uploader.domain.com', userName: 'abc123' })

then access this userName in the Uppy.js file and append it to the filename like: providerOptions: { s3: { endpoint: 'https://{service}.{region}.amazonaws.com', conditions: [], getKey: (req, filename) => userName +'/'+ filename } }

Thanks in advance.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
goto-bus-stopcommented, Oct 2, 2019

The “best” way currently is using headers.

On the client side, you can do:

uppy.use(AwsS3, {
  ...options,
  // should be called companionHeaders in a future release but this is what it's called right now
  serverHeaders: {
    'My-Meta-Header': 'username or something'
  }
})

On the server side, make sure you allow the header:

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allowed-Headers',
    'Authorization, Origin, Content-Type, Accept, My-Meta-Header')
  next()
})
app.use(uppy.app(uppyOptions))

You can access it in the getKey option:

providerOptions: {
  s3: {
    getKey: (req, filename) => {
      if (!req.headers['My-Meta-Header']) throw new Error('missing username')
      return req.headers['My-Meta-Header'] + '/' + filename
    },
  }
}

In the future we will hopefully support accessing Uppy metadata in getKey() directly to make this less annoying!

2reactions
chetanmenariacommented, Oct 4, 2019

@chetanmenaria

I have managed to make it work the way I want. Maybe this could be helpful for you:

    const uppy = Uppy({
      id: this.uppyId,
      autoProceed: false,
      debug: true,
      thumbnailGeneration: true,
      restrictions: {
        maxFileSize: false,
        allowedFileTypes: ['image/*', 'application/pdf']
      },
      meta: {
        modelId: this.modelId,
        collection: this.collection
      },
      onBeforeFileAdded: () => {
        Promise.resolve()
      },
      onBeforeUpload: files => {
        for (var prop in files) {
          files[prop].name = 'myfolder/' +  files[prop].name
          files[prop].meta.name = 'myfolder/' +  files[prop].meta.name
        }
        this.files = files
        Promise.resolve()
      }
    })

pay attention to onBeforeUpload.

@acrolink Kudos. It worked like a charm. Thank you very much 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Amazon S3 boto - how to create a folder? - Stack Overflow
In the aws console when looking at a bucket you can click "Create Folder" and it will make one, and they can be...
Read more >
Uploading objects - Amazon Simple Storage Service
When you upload a file to Amazon S3, it is stored as an S3 object. Objects consist of the file data and metadata...
Read more >
AWS S3 - Uppy
The @uppy/aws-s3 plugin can be used to upload files directly to an S3 bucket. Uploads can be signed using either Companion or a...
Read more >
Creating File System APIs - DreamFactory
SFTP (SSH File Transfer Protocol) is the secure version of FTP, ... your S3 data. Further, because the S3 API is native to...
Read more >
Generate txt file instead of gz hive to s3 - bpm kids
You can use the LOCATION clause in the CREATE TABLE to specify the location of external table data. ID FROM dual; 6 END;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found