Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@aws-sdk/client-s3 producing error on image upload #4626

Closed
3 tasks done
himyuni opened this issue Apr 7, 2023 · 4 comments
Closed
3 tasks done

@aws-sdk/client-s3 producing error on image upload #4626

himyuni opened this issue Apr 7, 2023 · 4 comments
Assignees
Labels
bug This issue is a bug. p3 This is a minor priority issue

Comments

@himyuni
Copy link

himyuni commented Apr 7, 2023

Checkboxes for prior research

Describe the bug

trying to upload a image to s3. the error it's generating is horrible, i tried to find the solution everywhere, nothing seems working. The generated error saying its UnknownError. how i am suppose to find the problem and fix it where aws is saying it's a unknown error :D.

SDK version number

@aws-sdk/package-name@3.306.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

14.9.1

Reproduction Steps

const client = new S3({
    region: process.env.AWS_S3_REGION,
    credentials: {
        ccessKeyId: process.env.AWS_ACCESS_KEY,
        secretAccessKey: process.env.AWS_SECRET_KEY
    }
})

const upload = async (name, file, bucket=process.env.AWS_S3_BUCKET) => {

    const uploader = {
        Bucket: bucket,
        Key: name,
        Body: Buffer.from(file.data)
    }

    const upload = await client.putObject(uploader, (err, data) => {
        console.log('callback')
        console.log(err)
        console.log(data)
    })

    return {
        etag: upload.ETag.replace(/\"/g,""),
        location: upload.Location,
        key: Key,
        bucket: bucket
    }
}

Observed Behavior

400: UnknownError
    at throwDefaultError (/app/node_modules/@aws-sdk/smithy-client/dist-cjs/default-error-    handler.js:8:22)
    at deserializeAws_restXmlPutObjectCommandError (/app/node_modules/@aws-sdk/client-s3/dist-cjs/protocols/Aws_restXml.js:5782:43)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async /app/node_modules/@aws-sdk/middleware-serde/dist-cjs/deserializerMiddleware.js:7:24
    at async /app/node_modules/@aws-sdk/middleware-signing/dist-cjs/middleware.js:14:20
    at async /app/node_modules/@aws-sdk/middleware-retry/dist-cjs/retryMiddleware.js:27:46
    at async /app/node_modules/@aws-sdk/middleware-flexible-checksums/dist-cjs/flexibleChecksumsMiddleware.js:58:20
    at async /app/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:7:26 {
        '$fault': 'client',
        '$metadata': {
        httpStatusCode: 400,
        requestId: '6ACVW778TJGP01YP',
        extendedRequestId:    'qv518QMX7msriUZgvRtEvysSdhAMKb2xUWUlEc8aOrkZSBB/Z7kQ2AAXLZU6KsUF4NOzLX01Fs4=',
        cfId: undefined,
        attempts: 1,
        totalRetryDelay: 0
    }
}

Expected Behavior

Should've uploaded the image.

Possible Solution

No response

Additional Information/Context

No response

@himyuni himyuni added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 7, 2023
@yenfryherrerafeliz yenfryherrerafeliz added p3 This is a minor priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Apr 10, 2023
@yenfryherrerafeliz yenfryherrerafeliz self-assigned this Apr 10, 2023
@yenfryherrerafeliz
Copy link
Contributor

Hi @himyuni, thanks for opening this issue. I agree that getting UnknownError as error message is not helpful. I will bring this into the team to discuss how the desserialization for this type of errors can be improved. However, something you can do as of right now is to log the response before it is parsed, and there you will the whole message that comes with the response.
Here is what you can do:

// 1 - First create a custom handler 
class MyCustomHandler extends NodeHttpHandler {
    handle(request, {abortSignal}) {
        const promise = super.handle(request, {abortSignal});

        return promise.then((response) => {
            const body = response.response.body;
            console.log((typeof body));
            (async () => {
                console.log(await Promise.resolve(body))
            })();
            return response;
        });
    }
}
// 2 - Then, use the custom handler in the client
const client = new S3({
    region: 'us-east-2',
    requestHandler: new MyCustomHandler()
})
// 3 - 
const upload = async (name, file, bucket=process.env.TEST_BUCKET) => {
    const uploader = {
        Bucket: bucket,
        Key: name,
        Body: Buffer.from(file)
    }
    const upload = await client.putObject(uploader);

    return {
        etag: upload.ETag.replace(/\"/g,""),
        location: '',
        key: name,
        bucket: bucket
    }
}
const file = fs.readFileSync('./image.png', {});
const response = await upload(process.env.TEST_KEY, file)

Please let me know if that helps.

Thanks!

@yenfryherrerafeliz yenfryherrerafeliz added the response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. label Apr 10, 2023
@himyuni
Copy link
Author

himyuni commented Apr 11, 2023

actually I fixed the issue. after 2 days of debugging. I found The error, it was in the previous line where I called the upload function. but don't know why aws was producing the error.

Previous Code
const fileName = client/avatar/${generateFileName()}.${fileExtension(reqFile.data)} -> this was the cause of the error
const avatar = await aws.s3.upload(fileName, reqFile.data)

After Fix
const fileName = client/avatar/${generateFileName()}.${fileExtension(reqFile.name)}
const avatar = await aws.s3.upload(fileName, reqFile.data)

The fileExtension function only take string as a param. this function could've generate the error and stopped the execution. But don't know why it was carrying forward the execution to the upload function.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. label Apr 12, 2023
@yenfryherrerafeliz
Copy link
Contributor

@himyuni thanks for letting me know, and I am glad you get it fixed. If you have any other issues feel free to reach out.

Thanks!

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. p3 This is a minor priority issue
Projects
None yet
Development

No branches or pull requests

2 participants