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

Custom metadata support #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ let options = {
region: "us-east-1",
accessKey: "your-access-key",
secretKey: "your-secret-key",
successActionStatus: 201
successActionStatus: 201,
metadata: {
latitude: '123.506239', // Becomes x-amz-meta-latitude onec in S3
longitude: '-23.045293',
photographer: 'John Doe'
}
}

RNS3.put(file, options).then(response => {
Expand Down Expand Up @@ -91,6 +96,7 @@ Arguments:
* `accessKey` **required** - Your S3 `AWSAccessKeyId`
* `secretKey` **required** - Your S3 `AWSSecretKey`
* `successActionStatus` - HTTP response status if successful, defaults to 201.
* `metadata` - Custom metadata to attach to your object

Returns an object that behaves like a promise. It also has a `progress` method on it which accepts a callback and will invoke the callback with the upload progress.

Expand Down
19 changes: 19 additions & 0 deletions src/Metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Metadata
*/

const metadataPrefix = 'x-amz-meta-'; // Lowercase due to aws requirements

export class Metadata {
static generate(options) {

let metadata = {};

if (options.metadata) {
Object.keys(options.metadata).forEach((k) => { metadata[metadataPrefix + k] = options.metadata[k] });
}

return metadata;

}
}
17 changes: 11 additions & 6 deletions src/RNS3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { Request } from './Request';
import { S3Policy } from './S3Policy';
import { Metadata } from './Metadata';

const EXPECTED_RESPONSE_KEY_VALUE_RE = {
key: /<Key>(.*)<\/Key>/,
Expand All @@ -29,17 +30,21 @@ export class RNS3 {
static put(file, options) {
options = Object.assign({}, options, {
key: (options.keyPrefix || '') + file.name,
contentType: file.type
contentType: file.type,
metadata: Metadata.generate(options)
});

let url = `https://${ options.bucket }.s3.amazonaws.com`;
let method = "POST";
let policy = S3Policy.generate(options);

return Request.create(url, method, policy)
.set("file", file)
.send()
.then(setBodyAsParsedXML);
}
let request = Request.create(url, method, policy);

Object.keys(options.metadata).forEach((k) => request.set(k, options.metadata[k]));

request.set('file', file);

return request
.send()
.then(setBodyAsParsedXML); }
}
12 changes: 10 additions & 2 deletions src/S3Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ const getPolicyParams = (options) => {
key: options.key,
region: options.region,
secretKey: options.secretKey,
successActionStatus: '' + (options.successActionStatus || DEFAULT_SUCCESS_ACTION_STATUS)
successActionStatus: '' + (options.successActionStatus || DEFAULT_SUCCESS_ACTION_STATUS),
metadata: options.metadata
}
}

Expand All @@ -89,7 +90,7 @@ const formatPolicyForRequestBody = (base64EncodedPolicy, signature, options) =>
}

const formatPolicyForEncoding = (policy) => {
return {
let policyForEncoding = {
"expiration": policy.expiration,
"conditions": [
{"bucket": policy.bucket},
Expand All @@ -102,6 +103,13 @@ const formatPolicyForEncoding = (policy) => {
{"x-amz-date": policy.date.amzDate}
]
}

Object.keys(policy.metadata).forEach((k) => {
let metadata = String(policy.metadata[k])
policyForEncoding.conditions.push({[k]: metadata});
})

return policyForEncoding;
}

const getEncodedPolicy = (policy) => {
Expand Down