Skip to content

Commit

Permalink
fix: honour the set region config (#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashsvmx committed Jul 11, 2022
1 parent 7ad7aea commit 55d0161
Show file tree
Hide file tree
Showing 4 changed files with 458 additions and 406 deletions.
2 changes: 2 additions & 0 deletions src/main/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,3 +791,5 @@ export class SelectResults {
}

}

export const DEFAULT_REGION = 'us-east-1'
28 changes: 20 additions & 8 deletions src/main/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import {
LEGAL_HOLD_STATUS, CopySourceOptions, CopyDestinationOptions, getSourceVersionId,
PART_CONSTRAINTS,
partsRequired,
calculateEvenSplits
calculateEvenSplits,
DEFAULT_REGION
} from './helpers.js'

import { signV4, presignSignatureV4, postPresignSignatureV4 } from './signing.js'
Expand Down Expand Up @@ -517,7 +518,7 @@ export class Client {
if (this.regionMap[bucketName]) return cb(null, this.regionMap[bucketName])
var extractRegion = (response) => {
var transformer = transformers.getBucketRegionTransformer()
var region = 'us-east-1'
var region = DEFAULT_REGION
pipesetup(response, transformer)
.on('error', cb)
.on('data', data => {
Expand Down Expand Up @@ -545,7 +546,7 @@ export class Client {
// obtained region.
var pathStyle = this.pathStyle && typeof window === 'undefined'

this.makeRequest({method, bucketName, query, pathStyle}, '', [200], 'us-east-1', true, (e, response) => {
this.makeRequest({method, bucketName, query, pathStyle}, '', [200], DEFAULT_REGION, true, (e, response) => {
if (e) {
if (e.name === 'AuthorizationHeaderMalformed') {
var region = e.Region
Expand Down Expand Up @@ -609,10 +610,9 @@ export class Client {
throw new errors.InvalidArgumentError(`Configured region ${this.region}, requested ${region}`)
}
}

// sending makeBucket request with XML containing 'us-east-1' fails. For
// default region server expects the request without body
if (region && region !== 'us-east-1') {
if (region && region !== DEFAULT_REGION) {
var createBucketConfiguration = []
createBucketConfiguration.push({
_attr: {
Expand All @@ -634,8 +634,20 @@ export class Client {
headers["x-amz-bucket-object-lock-enabled"]=true
}

if (!region) region = 'us-east-1'
this.makeRequest({method, bucketName, headers}, payload, [200], region, false, cb)
if (!region) region = DEFAULT_REGION

const processWithRetry = (err) =>{
if (err && (region === "" || region === DEFAULT_REGION)) {
if(err.code === "AuthorizationHeaderMalformed" && err.region !== ""){
// Retry with region returned as part of error
this.makeRequest({method, bucketName, headers}, payload, [200], err.region, false, cb)
}
return
}
cb && cb()
}

this.makeRequest({method, bucketName, headers}, payload, [200], region, false, processWithRetry)
}

// List of buckets created.
Expand All @@ -651,7 +663,7 @@ export class Client {
throw new TypeError('callback should be of type "function"')
}
var method = 'GET'
this.makeRequest({method}, '', [200], 'us-east-1', true, (e, response) => {
this.makeRequest({method}, '', [200], DEFAULT_REGION, true, (e, response) => {
if (e) return cb(e)
var transformer = transformers.getListBucketTransformer()
var buckets
Expand Down
4 changes: 2 additions & 2 deletions src/main/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { EventEmitter } from 'events'
import * as transformers from './transformers'
import { pipesetup, uriEscape } from './helpers'
import {DEFAULT_REGION, pipesetup, uriEscape} from './helpers'

// Notification config - array of target configs.
// Target configs can be
Expand Down Expand Up @@ -151,7 +151,7 @@ export class NotificationPoller extends EventEmitter {
if (queries.length > 0) {
query = `${queries.join('&')}`
}
const region = this.client.region || 'us-east-1'
const region = this.client.region || DEFAULT_REGION
this.client.makeRequest({ method, bucketName: this.bucketName, query }, '', [200], region, true, (e, response) => {
if (e) return this.emit('error', e)

Expand Down

0 comments on commit 55d0161

Please sign in to comment.