Skip to content

Commit

Permalink
add support for other endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidalizad committed Dec 9, 2024
1 parent daee5e6 commit 1d5964f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ var S3Adapter = require("@parse/s3-files-adapter");
var AWS = require("aws-sdk");

//Configure Digital Ocean Spaces EndPoint
const spacesEndpoint = new AWS.Endpoint(process.env.SPACES_ENDPOINT);
var s3Options = {
bucket: process.env.SPACES_BUCKET_NAME,
baseUrl: process.env.SPACES_BASE_URL,
Expand All @@ -293,7 +292,7 @@ var s3Options = {
s3overrides: {
accessKeyId: process.env.SPACES_ACCESS_KEY,
secretAccessKey: process.env.SPACES_SECRET_KEY,
endpoint: spacesEndpoint
endpoint: process.env.SPACES_ENDPOINT
}
};

Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class S3Adapter {
this._presignedUrlExpires = parseInt(options.presignedUrlExpires, 10);
this._encryption = options.ServerSideEncryption;
this._generateKey = options.generateKey;
this._endpoint = options.s3overrides?.endpoint;
// Optional FilesAdaptor method
this.validateFilename = options.validateFilename;

Expand Down Expand Up @@ -157,7 +158,8 @@ class S3Adapter {
await this.createBucket();
const command = new PutObjectCommand(params);
const response = await this._s3Client.send(command);
const location = `https://${this._bucket}.s3.${this._region}.amazonaws.com/${params.Key}`;
const endpoint = this._endpoint || `https://${this._bucket}.s3.${this._region}.amazonaws.com`;
const location = `${endpoint}/${params.Key}`;

return Object.assign(response || {}, { Location: location });
}
Expand Down
13 changes: 13 additions & 0 deletions spec/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ describe('S3Adapter tests', () => {
s3overrides: { endpoint: otherEndpoint },
};
const s3 = new S3Adapter(confObj);
expect(s3._endpoint).toEqual(otherEndpoint);
const endpointFromConfig = await s3._s3Client.config.endpoint();
expect(endpointFromConfig.protocol).toEqual('https:');
expect(endpointFromConfig.path).toEqual('/path');
Expand All @@ -161,6 +162,18 @@ describe('S3Adapter tests', () => {
expect(endpointFromConfig.query.foo).toEqual('bar');
});

it("should have undefined endpoint if no custom endpoint is provided", async () => {
const confObj = {
bucketPrefix: 'test/',
bucket: 'bucket-1',
secretKey: 'secret-1',
accessKey: 'key-1',
};
const s3 = new S3Adapter(confObj);
const endpoint = await s3._s3Client.config.endpoint?.();
expect(endpoint).toBeUndefined();
});

it('should accept options and overrides as args', () => {
const confObj = {
bucketPrefix: 'test/',
Expand Down

0 comments on commit 1d5964f

Please sign in to comment.