0
votes

Hi,

Please bear with me as I'm a newbie on this.

I'm trying to upload image files to an Amazon Web Services S3 bucket using NodeJS Knox client but I keep getting a 400 error every time I try. I think it could be an authorization issue as I can't find any errors on my code in NodeJS. I printed the knox client object to the console after it does the upload and I get this part in it, not sure if it's telling me that AWS is not authorizing me:

agent:
Agent {
domain: null,
_events: { free: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: { path: null },
requests: {},
sockets:
{ 'photogridjose.s3.amazonaws.com:443:::::::::':
[ TLSSocket {
_tlsOptions:
{ pipe: null,
secureContext: SecureContext { context: SecureContext {},
singleUse: true },
isServer: false,
requestCert: true,
* rejectUnauthorized: true,
session: undefined,
NPNProtocols: undefined,
ALPNProtocols: undefined,
requestOCSP: undefined },
_secureEstablished: false,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: null,
alpnProtocol: null,
authorized: false, *
authorizationError: null,
encrypted: true,
_events:
{ close:
[ [Function],
{ [Function: bound onceWrapper] listener: [Function] },
[Function: onClose] ],
end:
[ { [Function: bound onceWrapper] listener: [Function: onend] },
{ [Function: bound onceWrapper] listener: [Function: onHangUp] } ],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
secureConnect: { [Function: bound onceWrapper] listener: [Function] },
connect: { [Function: bound onceWrapper] listener: [Function] },
secure: [Function],
free: [Function: onFree],
agentRemove: [Function: onRemove] },
_eventsCount: 9,
connecting: true,
_hadError: false,
_handle:
TLSWrap {
bytesRead: 0,
_externalStream: [External],
fd: -1,
_parent:
TCP {
bytesRead: 0,
_externalStream: [External],
fd: -1,
reading: [Getter/Setter],
owner: [Circular],
onread: null,
onconnection: null,
writeQueueSize: 0 },
_parentWrap: undefined,
_secureContext: SecureContext { context: SecureContext {},
singleUse: true },
reading: false,
owner: [Circular],
onread: [Function: onread],


I used the AWSAccessKeyId & AWSSecretKey that I was given by AWS and changed the S3 bucket's policy so it's set to read/write for everyone.

This is my bucket policy doc:

{
 "Version": "2012-10-17",
 "Id": "Policy150032*******",
 "Statement": [
 {
  "Sid": "Stmt1500*********",
  "Effect": "Allow",
  "Principal": "*",
  "Action": [
   "s3:GetObject",
   "s3:PutObject",
   "*"
  ],
  "Resource": "arn:aws:s3:::Myphotogrid/*"
  }
 ]
}


Here's my CORS document:

<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
 <AllowedOrigin>* </AllowedOrigin>
 <AllowedMethod>GET </AllowedMethod>
 <AllowedMethod>POST </AllowedMethod>
 <AllowedMethod>PUT </AllowedMethod>
 <AllowedHeader>Authorization </AllowedHeader>
</CORSRule>
</CORSConfiguration>

Here's the S3 Bucket's log for that transaction:

<Error>
 <Code>AccessDenied</Code>
 <Message>Access Denied</Message>
 <RequestId>37329C57F01BEC29</RequestId>
 <HostId>   zYNm+4zorTHrNnmHBAYPVpFfkJJ3VL1qtJ+PI9vJPRHhCb7vGhhav4YNRlbbakRJlpyab6Gfcn0=  </HostId>
</Error>

Does anyone here have experience using AWS S3 buckets that could help?.

1
400 is a bad request. This probably means your request data is invalid. Not an authorization issue. 403 would be the one you'd see if you were unauthorized. Check here for a list of error responses.Lansana Camara
I deleted my S3 bucket and made a new one and now I'm getting a 307 error which is a Temporary redirect, probably because the new bucket was just created. If this fixes the issue I'll update. It seems it was a permission issue with the S3 bucket itself.Jose Vasconcellos

1 Answers

0
votes

Made a new S3 bucket and deleted the old one, made sure permissions are right for the public to read/write to bucket,and selected the server closest to my location to store it which both solved the 400 & then later 307 errors I was getting. I'm getting a 200 success response now and files are being uploaded to my S3 bucket without problems.