UnknownError from using AWSJavaScriptSDK glacier - node.js

I am pulling my hair for 2 hours trying to find what's going on. I've got UnknownError from making use of AWSJavaScriptSDK to upload file to AWS Glacier.
var AWS, buffer, config, glacier, params, vaultName;
AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: "KEY_ID",
secretAccessKey: "SECRETE",
region: "ap-southeast-2"
});
glacier = new AWS.Glacier();
vaultName = "arn:aws:glacier:ap-southeast-2:VAULT_ID:vaults/VAULT_NAME";
buffer = new Buffer(2.5 * 1024 * 1024);
params = {
vaultName: vaultName,
body: buffer
};
glacier.uploadArchive(params, function(err, data) {
if (err) {
return console.log("Error uploading archive!", err);
}
});
The result in the console is bellow:
Error uploading archive! { [UnknownError: 400]
message: '400',
code: 'UnknownError',
statusCode: 400,
time: Mon May 04 2015 00:03:20 GMT+1000 (AEST),
retryable: false,
retryDelay: 30 }
I've tried:
Using root api keys and secrete ( try to rule out permission issue )
not setting the api keys or set a wrong ones
Upload file from fs.readFile instead of using Buffer
include accountId: "-" to the param variable
All attempts result in the same error.

Answer my question. The vaultName should just be the name instead of the whole arn string.
The err message did not even say it !

Related

AWS node.js SDK getting 'Access Denied' using AWS example

I'm new at AWS, maybe I'm missing something obvious so help is required.
I have 2 version of code, the only different is passing bucket as a 4 chars string vs 5 chars. getting different response from aws.
why is that?
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
s3.createBucket({Bucket: 'node4'}, function() {
var params = {Bucket: 'node4', Key: 'myKey', Body: 'Hello!'};
s3.putObject(params, function(err, data) {
if (err)
console.log(err)
else
console.log("Successfully uploaded data to myBucket/myKey");
});
});
running the app.js:
➜ aws node app.js
{ [AllAccessDisabled: All access to this object has been disabled]
message: 'All access to this object has been disabled',
code: 'AllAccessDisabled',
region: null,
time: Fri Feb 05 2016 20:45:11 GMT+0200 (IST),
requestId: 'somerequestId',
extendedRequestId: 'someextendedRequestId',
statusCode: 403,
retryable: false,
retryDelay: 30 }
second code:
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
s3.createBucket({Bucket: 'node4e'}, function() {
var params = {Bucket: 'node4e', Key: 'myKey', Body: 'Hello!'};
s3.putObject(params, function(err, data) {
if (err)
console.log(err)
else
console.log("Successfully uploaded data to myBucket/myKey");
});
});
running the app.js:
➜ aws node app.js
Successfully uploaded data to myBucket/myKey
the only different is passing bucket as a 4 chars string vs 5 chars
Actually the only difference is that you don't have write access to the bucket named 'node4' and you do have access to a bucket named 'node4e'. Did you check to see if both buckets were actually created successfully? I notice you aren't checking for errors in the createBucket() call, just the putObject() call.
Those are fairly generic bucket names you are using, I wouldn't be surprised if the one that is failing, 'node4', is already in use by another AWS account.

NoSuchBucketPolicy error when trying to generate Bucket Policy

I'm trying out AWS S3 for the first time and I wrote the following function to generate a bucket policy.
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Load configuration
AWS.config = new AWS.Config();
AWS.config.accessKeyId = sails.config.accessKeyId;
AWS.config.secretAccessKey = sails.config.secretAccessKey;
AWS.config.region = sails.config.region
// Create S3 object
var s3 = new AWS.S3();
// Defining the required parameters
var params = {
Bucket: "bucket-name-here"
};
s3.getBucketPolicy(params, function(error, date) {
if(error) {
// An error occurred
console.log("Error\n" + error);
return res.json({
message: "Error",
'error': error
});
} else {
// Successful
console.log("Data\n" + date);
return res.json({
message: "Successful",
'data': date
});
}
});
But the response is always NoSuchBucketPolicy: The bucket policy does not exist
I tried uploading a test file into the bucket, listing all buckets and it both worked as expected. What is wrong with the code?
Your code doesn't "generate" a bucket policy... it tries to fetch the existing policy of a bucket. Buckets don't have a policy until you create one, so this error would be normal in that case.
Error Code: NoSuchBucketPolicy
Description: specified bucket does not have a bucket policy.
HTTP Status Code: 404 Not Found
http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html

How to corretly upload to Amazon S3 with meteor slingshot?

I am trying to upload to S3 and the error I keep getting in the web console is
Exception in delivering result of invoking 'slingshot/uploadRequest': TypeError: >Cannot read property 'response' of undefined
Here is my sever side code:
Slingshot.fileRestrictions("Test1", {
allowedFileTypes: ["image/png", "image/jpeg", "image/gif"],
maxSize: 10 * 1024 * 1024 // 10 MB (use null for unlimited)
});
Slingshot.createDirective("Test1", Slingshot.S3Storage, {
AWSAccessKeyId: "Key",
AWSSecretAccessKey: "Key",
bucket: "bucketname",
acl: "public-read",
authorize: function () {
//Deny uploads if user is not logged in.
},
key: function (file) {
//Store file into a directory by the user's username.
return file.name;
}
Client side code:
Template.first.events({
'change .fileInput': function(event, template) {
event.preventDefault();
var uploader = new Slingshot.Upload("Test1");
var docc = document.getElementById('fileup').files[0];
console.log(docc);
uploader.send(docc, function (error){
if (error) {
console.error('Error uploading', uploader.xhr.response);
alert (error);
}
else{
console.log("Worked!");
}
});
}
Help would be much appreciated!
It also appears important to reference the region in the createDirective command block, e.g.
Slingshot.createDirective("Test1", Slingshot.S3Storage, {
bucket: "bucketname",
region: "eu-west-1"
...
Without the region explicitly specified, all I get is a '400 - Bad Request".
The AWSAccessKeyId and AWSSecretAccessKey should go in a separate settings.json file that is restricted to your server's deployment and excluded from source code management (github).
settings.json:
{
"AWSAccessKeyId": "<insert key id here>",
"AWSSecretAccessKey": "<insert access key here>"
}
Typically you'll have a settings file per environment (dev, acpt, live) all as part of the same code-base, and selected at runtime for the environment required.
Failing on below line. xhr is null
console.error('Error uploading', uploader.xhr.response);
Log the error like below and debug from there
console.error('Error uploading', error);

AWS node.js not creating S3 bucket?

I'm trying to use the basic tutorial to create an S3 bucket as follows
var AWS = require('aws-sdk');
AWS.config.loadFromPath('./myawsconfig.json');
AWS.config.update({region: 'eu-west-1'});
var s3 = new AWS.S3();
s3.client.createBucket({Bucket: 'pBucket'}, function() {
var data = {Bucket: 'pBucket', Key: 'myKey', Body: 'Hello!'};
s3.client.putObject(data, function(err, data) {
if (err) {
console.log("Error uploading data: ", err);
} else {
console.log("Successfully uploaded data to myBucket/myKey");
}
});
});
But I'm receiving the following error
node createbucket.js
Error uploading data: { [NoSuchBucket: The specified bucket does not exist]
message: 'The specified bucket does not exist',
code: 'NoSuchBucket',
name: 'NoSuchBucket',
statusCode: 404,
retryable: false }
I just ran into this problem, apparently the Node.js tutorial code doesn't compile. I got a var Object doesn't have createBucket method.
This worked:
var AWS = require('aws-sdk');
AWS.config.loadFromPath('./credentials.json');
// Set your region for future requests.
AWS.config.update({region: 'us-east-1'});
// Create a bucket and put something in it.
var s3 = new AWS.S3();
s3.client.createBucket({Bucket: 'hackathon-test'}, function() {
var data = {Bucket: 'hackathon-test', Key: 'myKey', Body: 'Hello!'};
s3.client.putObject(data, function(err, data) {
if (err) {
console.log("Error uploading data: ", err);
} else {
console.log("Successfully uploaded data to myBucket/myKey");
}
});
});
I had this issue, discovering that my api-user didn't have permission to create the bucket.
Slightly more thorough error checking revealed this...
s3.client.createBucket({Bucket: 'someBucket'}, function(err) {
if (err) {
console.log("Error creating bucket: ", err);
} else {
console.log("Successfully created bucket 'someBucket'");
}
// ...
According to aws S3 bucket name restrictions.
your bucket name shouldn't contain any uppercase letter. so 'pBucket' is invalid.
http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
The rules for DNS-compliant bucket names are:
Bucket names must be at least 3 and no more than 63 characters long.
Bucket names must be a series of one or more labels. Adjacent labels are separated by a single period (.). Bucket names can contain
lowercase letters, numbers, and hyphens. Each label must start and end
with a lowercase letter or a number.
Bucket names must not be
formatted as an IP address (e.g., 192.168.5.4).
When using virtual
hosted–style buckets with SSL, the SSL wild card certificate only
matches buckets that do not contain periods. To work around this, use
HTTP or write your own certificate verification logic.
A couple of pointers that I missed and someone may find useful
if you set the region as part of the S3 object var s3 = new AWS.S3({region: 'us-west-1'}); then the call will fail (in my experience).
You can therefore set the region via either
a) AWS.config.update({ region: 'eu-west-1' });
b) as part of the params on createBucket
s3.createBucket({
Bucket: bucketName,
CreateBucketConfiguration: {
LocationConstraint: "eu-west-1"
}
}, function () {
also, watch out for caps or underscores in the bucket name as that took an hour of my life too (DNS compliant only).

Can't upload images in nodejs using aws-sdk

I've tried using aws-sdk and knox and I get status code 301 trying to upload images. I get status code 301 and message - 'The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint. This works in php.
AWS.config.loadFromPath(__dirname + '/config/config.json');
fs.readFile(source, function (err, data) {
var s3 = new AWS.S3();
s3.client.createBucket({Bucket: 'mystuff'}, function() {
var d = {
Bucket: 'mystuff',
Key: 'img/test.jpg',
Body: data,
ACL: 'public-read'
};
s3.client.putObject(d, function(err, res) {
if (err) {
console.log("Error uploading data: ", err);
callback(err);
} else {
console.log("Successfully uploaded data to myBucket/myKey");
callback(res);
}
});
});
});
I actually solved this problem. In your config you have to have a region, since my bucket was "US Standard", I left my region blank and it worked.
config.json -
{ "accessKeyId": "secretKey", "secretAccessKey": "secretAccessKey", "region": ""}
go to s3 management console select one of your files and click on proporties - > look at the file link.
US standard
https://s3.amazonaws.com/yourbucket/
host in your console window
yourbucket.s3.amazonaws.com/
us-west-1
https://s3-us-west-1.amazonaws.com/yourbucket/
host in your console window
yourbucket.s3-us-west-1.amazonaws.com/
Did you try .send()?
I can upload to S3 by below code.
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/AWSRequest.html
var s3object = {Bucket: 'mystuff', Key: name, Body : data['data']};
s3.client.putObject(s3object).done(function(resp){
console.log("Successfully uploaded data");
}).fail(function(resp){
console.log(resp);
}).send();
I have the same problem with the new SDK and solved it by setting the endpoint option explicitly.
Reference : http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor_details
Snippet:
var AWS = require('aws-sdk');
var s3 = new AWS.S3({ endpoint :'https://s3-your-region-varies.amazonaws.com' }),
myBucket = 'your-bucket-name';
var params = {Bucket: myBucket, Key: 'myUpload', Body: "Test"};
s3.putObject(params, function(err, data) {
if (err) {
console.log(err)
} else {
console.log("Successfully uploaded data to "+myBucket+"/testKeyUpload");
}
});
Alternatively, you can solve this by setting the region in your config file and you just have to be precise of your region name.

Resources