andrewrk / node-s3-client

high level amazon s3 client for node.js
MIT License
1k stars 305 forks source link

Unexpected key 'Prefix' found in params #174

Closed DylanAndrews closed 7 years ago

DylanAndrews commented 7 years ago

When I attempt to deploy with a prefix in the s3Params I am getting the following error. Unexpected key 'Prefix' found in params

Here is the code for the deploy.

  uploadAssets (config) {
    var client = s3.createClient({ s3Options: config })
    console.log('Asset Upload: Started ' + (new Date()).getTime())
    var uploader = client.uploadDir(this.assetParams)
    console.log(this.assetParams)
    uploader.on('progress', () => {
      if (uploader.progressTotal === 0) { return }

      // TODO: Add a proper progress bar
      var percentage = ((uploader.progressAmount / uploader.progressTotal) * 100).toFixed()
      console.log('Assets Upload: Progress', percentage + '%')
    })

    return new Promise((resolve, reject) => {
      uploader.on('end', () => {
        console.log('Asset Upload: Done ' + (new Date()).getTime())
        resolve()
      })

      uploader.on('error', (err) => {
        console.error('Asset Upload: Error', err.stack)
        reject()
      })
    })
  }

And here are the s3 params

{
     ACL: 'public-read',
     Bucket: 'my-bucket',
     Prefix: 'analytics/'
}

When I go to the suggested url in the documentation I also see that there is nothing regarding a prefix property. Do you see anything wrong with my code or have any recommendations for what I should try? This deploy script works fine as long as the prefix isn't there, so I'm not sure what else the problem could be.

DylanAndrews commented 7 years ago

A member of my team just directed me to the cause of my issues.

I was adding Prefix to this.assetParams.s3Params,

    // Skip uploading HTML files
    if (/html|htm/.test(ext)) {
      params = null
    } else {
      params = _.assign({}, this.assetParams.s3Params, type, delivery)
    }

which is a valid top-level config for uploadDir/putObject. It is not, however, a valid config for an individual file. That leads to the “Unexpected key ‘Prefix’ found in params” error. The solution would be this.

    // Skip uploading HTML files
    if (/html|htm/.test(ext)) {
      params = null
    } else {
      params = _.assign({}, _.pick(this.assetParams.s3Params, ['ACL', 'Bucket']), type, delivery)
    }