keystonejs / keystone-storage-adapter-s3

⚠️ Archived - Legacy S3 Storage Adapter for KeystoneJS
MIT License
17 stars 55 forks source link

"Field errors" #9

Closed jstockwin closed 6 years ago

jstockwin commented 8 years ago

I'm currently failing to make S3 work at all...

I'll paste my entire model below.

Firstly, I can't always create a new item, which only requires name to be entered in the initial form. All I get then is "Unknown Error". It seems to occur when creating the first item. A page refresh fixes this.

After this, when trying to upload a file, I almost always get a flash message saying "Field errors", and nothing more. There is nothing logged anywhere, and my pre-save hook doesn't seem to be reached.

I'm not really sure how to get at more detailed errors either... help!

var keystone = require('keystone');
var Types = keystone.Field.Types;

/**
 * File Model
 * ==========
 */

var File = new keystone.List('File', {
    autokey: { path: 'slug', from: 'name', unique: true },
    track: true,
});

var storage = new keystone.Storage({
    adapter: require('keystone-storage-adapter-s3'),
    s3: {
        path: process.env.S3_BUCKET,
        region: process.env.S3_REGION,
        headers: {
            'x-amz-acl': 'public',
        },
    },
    schema: {
        bucket: true, // optional; store the bucket the file was uploaded to in your db
        etag: true, // optional; store the etag for the resource
        path: true, // optional; store the path of the file in your db
        url: true, // optional; generate & store a public URL
    },
});

File.add({
    name: { type: String, required: true },
    file: {
        type: Types.File,
        storage: storage,
        filename: function (item, file) {
            return encodeURI(item._id + '-' + item.name);
        },
        format: function (item, file) {
            return '<pre>' + JSON.stringify(file, false, 2) + '</pre>'
                + '<img src="' + file.url + '" style="max-width: 300px">';
        },
    },
    link: { type: Types.Url, note: 'This will be automatically populated once you\'ve uploaded a file.' },
});

File.schema.pre('save', function (next) {
    console.log(this);
    if (this.file && this.file.url) {
        this.link = this.file.url;
    }
    next();
});

File.defaultColumns = 'name';
File.register();
gabek commented 7 years ago

I'm seeing this same error and I have no idea where to look after random debugging :\

davidmrnustik commented 7 years ago

I get the same error when I run KeystoneJS from docker container. If I run KeystoneJS with the same File.js configuration from a local machine (not docker), it works and a file is uploaded to S3.

@JedWatson Should I have installed aws-cli on machine? Because I have it on local, but not in container.

davidmrnustik commented 7 years ago

I've installed and configure aws on Docker container and Field errors still persist.

stevenkaspar commented 7 years ago

I had the 'Field Errors' problem and it was because I had not added the Amazon S3 Permission to my IAM User's Group

screen shot 2017-11-07 at 6 45 52 pm
mrprkr commented 7 years ago

My understanding of this issue was to do with certain AWS regions now requiring checksum signing before upload. The library that this uses to upload does not support the new AWS skd requirements and some quick research looks like the project has been abandoned. EU-West-2 (London) and AP-southeast-2 (Sydney) both have the new requirements.

As an aside @stevenkaspar I’d suggest using a more specific permission set on AWS rather than full access. You can build policies to limit it to a single bucket.

stevenkaspar commented 7 years ago

@mrprkr Thanks for the heads up - relatively new to AWS. Went ahead and created a new policy

So should it be updated to use aws-sdk/clients/s3? I am planning to use this a lot so I can update it if it should be

mrprkr commented 7 years ago

I haven’t looked at this in almost a year, from memory there wasn’t good node support from AWS when they changed the requirements. I think this should be a pretty straight forward pull request, I’m on my iPad and can’t easily look at the code but there’s a library in here that handles the upload. This library has been abandoned and doesn’t support the new AWS-SDK. Updating the upload part of this module should flex the field errors issue.

mrprkr commented 7 years ago

FYI the library in question is Knox

bflopez commented 6 years ago

@stevenkaspar what did you end up doing?

stevenkaspar commented 6 years ago

@bflopez I ended up forking the repo and created a PR but it hasn't been accepted

https://github.com/keystonejs/keystone-storage-adapter-s3/pull/29 https://github.com/stevenkaspar/keystone-storage-adapter-s3

bflopez commented 6 years ago

@stevenkaspar Saw that shortly after commenting. Swapped in your code and it works like a charm. Thank you.

Avcajaraville commented 6 years ago

@stevenkaspar Alright mate, your solution just works !! <3 <3 <3

mikehazell commented 6 years ago

This issue has been resolved in v2.0.0. See #35