I'm trying to call the setPromisesAWS.config.setPromisesDependency config function but my config object doesn't seem to have the setPromisesDependency property.
Am I missing something?
AWS.config.setPromisesDependency(bluebird)
// Error : AWS.config.setPromisesDependency is not a function
I have version 2.2.48 installed via bower.
Any help would be appreciated.
Hi @dhatawesomedude
Thanks for your question. Promises are not supported in the SDK prior to version 2.3.0. If you update to 2.3.0 or later, the setPromisesDependency property will be on AWS.config.
Thanks @LiuJoyceC
Updating my version fixed this issue, but I still cannot call .promise on s3.upload(). The promise property is undefined. Any idea how to fix that?
Hi @dhatawesomedude
Promises are currently only supported on operations that return a Request object. Since s3.upload is a custom function that returns an instance of ManagedUpload rather than Request, promises are not currently supported for that operation. However, this is a feature request that we are looking into.
As a current workaround, if you know your file's size, you can use the putObject operation for file sizes <= 5MB, or use the multipart upload operations for file sizes greater than 5MB. The SDK does support promises on all of these operations. You could also create the promise:
var s3UploadPromise = new Promise(function(resolve, reject) {
s3.upload(params, function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
I hope that helps!
Another way to do this in the meanwhile is to use es6-promisify at https://www.npmjs.com/package/es6-promisify. Here's an example:
const params = { ... }
await promisify(s3.upload, s3)(params);
... or if you're not using async/await ...
const params = { ... }
promisify(s3.upload, s3)(params).then(fn).catch(fn);
Also, if you attempt to use promises with this and do not pass configuration object, it will throw an unhandled promise rejection! Definitely a bug?
(node:38374) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): MissingRequiredParameter: Missing required key 'Bucket' in params
Hi @niftylettuce
For your above comment about the unhandled promise rejection, are you referring to when you use es6-promisify? Are you referring to when you use the SDK promise support in the PR I submitted? I've tried to replicate this but have not gotten any rejections thrown (it's expected for the promise to be rejected if not all required params are present, but it shouldn't be getting thrown). Could you provide the exact code you are using to replicate this error? Thanks.
Referring to SDK in library.
On Oct 17, 2016 3:42 PM, "Joyce Liu" [email protected] wrote:
Hi @niftylettuce https://github.com/niftylettuce
For your above comment about the unhandled promise rejection, are you
referring to when you use es6-promisify? Are you referring to when you
use the SDK promise support in the PR I submitted? I've tried to replicate
this but have not gotten any rejections thrown (it's expected for the
promise to be rejected if not all required params are present, but it
shouldn't be getting thrown). Could you provide the exact code you are
using to replicate this error? Thanks.—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/aws/aws-sdk-js/issues/1076#issuecomment-254311807,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAf7hbREmEPLc0EU4ZQuLvROndbYBjOAks5q08-5gaJpZM4JWdZl
.
@niftylettuce
Are you using Node v6.6.0 or higher? As of Node v6.6.0, a warning will be emitted when a promise is rejected and a catch handler isn't attached to the promise. It is not throwing an error, and execution will not stop. This is a new feature of Node, not a bug in the SDK.
https://news.ycombinator.com/item?id=12509832
Yes I know that, I'm saying that there's somewhere in the AWS SDK where there is a promise that is not wrapped with a try catch. In my code I wrapped it with a try catch and STILL got an unhandled promise rejection. I'm on latest Node, which is 6.6.x.
Promises don't work with try-catch blocks because they don't throw errors. They reject the errors, and you'll need to add a catch handler to the promise to handle the error. The SDK's promises are designed to reject any errors that would normally get passed into the error-first callback when a callback is used instead of a promise. In the case of callbacks, if you don't pass in the required params, an error will be passed into your callback, and you as the user can decide how you want to handle the error in your callback. If you don't write explicit logic in your callback to handle the error, then the error is silently swallowed (and the SDK cannot control what happens in the user-supplied callback). Similarly for promises, it's up to the user to add a catch handler to handle rejected promises. If you don't add a catch handler, then it is analogous to receiving the error in a callback but not writing any error-handling logic in the callback. The SDK cannot handle the error for you because then the user will no longer be able to choose how to handle the error.
There are no unhandled promises somewhere in the internals of the SDK. The SDK does not generate promises anywhere unless you explicitly call the .promise() method on the AWS.Request class (and in the PR, a few additional classes), in which case the method returns the raw promise to you to handle as you see fit. If then and catch handlers are not added by the user, then the behavior is consistent with any regular promises outside of the SDK, which is that rejections and resolutions get swallowed silently. Does that make sense?
Yes it does but I had await in front of the promise.. I'll try again...
On Oct 17, 2016 8:23 PM, "Joyce Liu" [email protected] wrote:
Promises don't work with try-catch blocks because they don't throw errors.
They reject the errors, and you'll need to add a catch handler to the
promise to handle the error. The SDK's promises are designed to reject any
errors that would normally get passed into the error-first callback when a
callback is used instead of a promise. In the case of callbacks, if you
don't pass in the required params, an error will be passed into your
callback, and you as the user can decide how you want to handle the error
in your callback. If you don't write explicit logic in your callback to
handle the error, then the error is silently swallowed (and the SDK cannot
control what happens in the user-supplied callback). Similarly for
promises, it's up to the user to add a catch handler to handle rejected
promises. If you don't add a catch handler, then it is analogous to
receiving the error in a callback but not writing any error-handling logic
in the callback. The SDK cannot handle the error for you because then the
user will no longer be able to choose how to handle the error.
There are no unhandled promises somewhere in the internals of the SDK. The
SDK does not generate promises anywhere unless you explicitly call the
.promise() method on the AWS.Request class (and in the PR, a few
additional classes), in which case the method returns the raw promise to
you to handle as you see fit. If then and catch handlers are not added by
the user, then the behavior is consistent with any regular promises outside
of the SDK, which is that rejections and resolutions get swallowed
silently. Does that make sense?—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/aws/aws-sdk-js/issues/1076#issuecomment-254372552,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAf7hcaHMJB93tTzJ8YkE-1JoQq2vpb_ks5q1BGOgaJpZM4JWdZl
.
As an update, I believe promises are now supported on ManagedUploads so upload can be used instead of putObject if so desired: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3/ManagedUpload.html#promise-property
This is still an issue though. The following code will result in an unhandled promise rejection. There is definitely a core bug in the AWS-SDK.
try {
const s3 = new AWS.S3({}); // of course you put config here
const data = await s3.upload().promise(); // of course you'd put an object in upload as arg
} catch (err) {
throw err;
}
Results in the following:
(node:28391) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: params.Body is required
(node:28391) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
cc @LiuJoyceC @MrHubble
@niftylettuce I believe you're rethrowing the error inside of an async function. SDK constructors do not use promises in any way. Let's keep the discussion to #1650
One last response and then I'll leave it for you to discuss in https://github.com/aws/aws-sdk-js/issues/1650 as suggested by @jeskew
@niftylettuce I am using code similar to you and am not getting an error:
````js
var AWS = require('aws-sdk/dist/aws-sdk-react-native');
AWS.config.update({
accessKeyId: myKeyId,
secretAccessKey: mySecretKey,
region: myRegion
})
var s3 = new AWS.S3()
var params = {
Bucket: "mybucket",
Key: myKeyPrefix,
Body: myBody,
ContentType: 'image/png',
ContentEncoding: 'base64',
ACL: 'public-read'
}
let putObjectPromise = await s3.upload(params).promise()
let location = putObjectPromise.Location
````
Good luck.
@MrHubble if there is an error in the s3.upload().promise(), how would you handle it on your implementation?
@arvi You could use try catch block
@arvi
var s3 = new AWS.S3({apiVersion: '2006-03-01', region: 'us-west-2'});
var params = {
Bucket: 'bucket',
Key: 'example2.txt',
Body: 'Uploaded text using the promise-based method!'
};
var putObjectPromise = s3.putObject(params).promise();
putObjectPromise.then(function(data) {
console.log('Success');
}).catch(function(err) {
console.log(err);
});
@arvi
var s3 = new AWS.S3({apiVersion: '2006-03-01', region: 'us-west-2'}); var params = { Bucket: 'bucket', Key: 'example2.txt', Body: 'Uploaded text using the promise-based method!' }; var putObjectPromise = s3.putObject(params).promise(); putObjectPromise.then(function(data) { console.log('Success'); }).catch(function(err) { console.log(err); });
try put await before s3.putObject(..., like this:
var putObjectPromise = await s3.putObject(params).promise();
if you have a parent function, it must be async, otherwise it will not work
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.
Most helpful comment
Hi @dhatawesomedude
Promises are currently only supported on operations that return a Request object. Since
s3.uploadis a custom function that returns an instance of ManagedUpload rather than Request, promises are not currently supported for that operation. However, this is a feature request that we are looking into.As a current workaround, if you know your file's size, you can use the
putObjectoperation for file sizes <= 5MB, or use the multipart upload operations for file sizes greater than 5MB. The SDK does support promises on all of these operations. You could also create the promise:I hope that helps!