From 8fc33a230e58a4dc965a2cab0ab8db5c406fccf3 Mon Sep 17 00:00:00 2001 From: Aladin Bouzerd Date: Thu, 18 May 2017 17:55:57 +0800 Subject: [PATCH] Allow s3 urls to be set as path instead of subdomain --- src/RNS3.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/RNS3.js b/src/RNS3.js index de17e04f..c25e0f58 100644 --- a/src/RNS3.js +++ b/src/RNS3.js @@ -5,8 +5,6 @@ import { Request } from './Request' import { S3Policy } from './S3Policy' -const AWS_DEFAULT_S3_HOST = 's3.amazonaws.com' - const EXPECTED_RESPONSE_KEY_VALUE_RE = { key: /(.*)<\/Key>/, etag: /"?([^"]*)"?<\/ETag>/, @@ -30,6 +28,14 @@ const setBodyAsParsedXML = (response) => }) export class RNS3 { + static getAwsUrl(options) { + if(options.awsUrl) { + return options.awsUrl; + } + + return `s3${options.region ? `-${options.region}` : ''}.amazonaws.com`; + } + static put(file, options) { options = { ...options, @@ -38,7 +44,9 @@ export class RNS3 { contentType: file.type } - const url = `https://${options.bucket}.${options.awsUrl || AWS_DEFAULT_S3_HOST}` + const url = options.urlAsPath ? + `https://${RNS3.getAwsUrl(options)}/${options.bucket}` : + `https://${options.bucket}.${RNS3.getAwsUrl(options)}` const method = "POST" const policy = S3Policy.generate(options)