Skip to content
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
},
"homepage": "https://github.com/scality/S3#readme",
"dependencies": {
"@aws-sdk/client-s3": "^3.908.0",
"@aws-sdk/credential-providers": "^3.864.0",
"@azure/storage-blob": "^12.28.0",
"@hapi/joi": "^17.1.1",
"arsenal": "git+https://github.com/scality/Arsenal#8.2.35",
"async": "2.6.4",
"aws-sdk": "^2.1692.0",
"bucketclient": "scality/bucketclient#8.2.7",
"bufferutil": "^4.0.8",
"commander": "^12.1.0",
Expand Down
176 changes: 85 additions & 91 deletions tests/functional/aws-node-sdk/lib/utility/bucket-util.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,87 @@
const AWS = require('aws-sdk');
AWS.config.logger = console;
const { S3 } = require('aws-sdk');
const {
S3Client,
HeadBucketCommand,
CreateBucketCommand,
DeleteBucketCommand,
ListObjectVersionsCommand,
DeleteObjectCommand,
ListBucketsCommand,
} = require('@aws-sdk/client-s3');
const projectFixture = require('../fixtures/project');
const getConfig = require('../../test/support/config');

class BucketUtility {
constructor(profile = 'default', config = {}) {
constructor(profile = 'default', config = {}, unauthenticated = false) {
const s3Config = getConfig(profile, config);

this.s3 = new S3(s3Config);
this.s3.config.setPromisesDependency(Promise);
this.s3.config.update({
maxRetries: 0,
});
if (unauthenticated) {
this.s3 = new S3Client({
...s3Config,
maxAttempts: 0,
credentials: { accessKeyId: '', secretAccessKey: '' },
forcePathStyle: true,
signer: { sign: async request => request },
});
}
else {
this.s3 = new S3Client({
...s3Config,
maxAttempts: 0,
});
}
}

bucketExists(bucketName) {
return this.s3
.headBucket({ Bucket: bucketName })
.promise()
return this.s3.send(new HeadBucketCommand({ Bucket: bucketName }))
.then(() => true)
.catch(err => {
if (err.code === 'NotFound') {
if (err.name === 'NotFound') {
return false;
}
throw err;
});
}

createOne(bucketName) {
return this.s3
.createBucket({ Bucket: bucketName })
.promise()
.then(() => bucketName);
return this.s3.send(new CreateBucketCommand({ Bucket: bucketName }))
.then(() => bucketName)
.catch(err => {
throw err;
});
}

createOneWithLock(bucketName) {
return this.s3
.createBucket({
Bucket: bucketName,
ObjectLockEnabledForBucket: true,
})
.promise()
.then(() => bucketName);
return this.s3.send(new CreateBucketCommand({
Bucket: bucketName,
ObjectLockEnabledForBucket: true,
})).then(() => bucketName);
}

createMany(bucketNames) {
const promises = bucketNames.map(bucketName =>
this.createOne(bucketName),
);

return Promise.all(promises);
}

createRandom(nBuckets = 1) {
if (nBuckets === 1) {
const bucketName = projectFixture.generateBucketName();

return this.createOne(bucketName);
}

const bucketNames = projectFixture
.generateManyBucketNames(nBuckets)
.sort(() => 0.5 - Math.random()); // Simply shuffle array

.sort(() => 0.5 - Math.random());
return this.createMany(bucketNames);
}

deleteOne(bucketName) {
return this.s3.deleteBucket({ Bucket: bucketName }).promise();
return this.s3.send(new DeleteBucketCommand({ Bucket: bucketName }));
}

deleteMany(bucketNames) {
const promises = bucketNames.map(bucketName =>
this.deleteOne(bucketName),
);

return Promise.all(promises);
}

Expand All @@ -84,68 +90,57 @@ class BucketUtility {
* @param bucketName
* @returns {Promise.<T>}
*/

async empty(bucketName, BypassGovernanceRetention = false) {
empty(bucketName, BypassGovernanceRetention = false) {
const param = {
Bucket: bucketName,
};

const listedObjects = await this.s3.listObjectVersions(param).promise();

for (const version of listedObjects.Versions) {
if (version.Key.endsWith('/')) {
continue;
}

await this.s3
.deleteObject({
Bucket: bucketName,
Key: version.Key,
VersionId: version.VersionId,
...(BypassGovernanceRetention && {
BypassGovernanceRetention,
}),
})
.promise();
}

for (const version of listedObjects.Versions) {
if (!version.Key.endsWith('/')) {
continue;
}

await this.s3
.deleteObject({
Bucket: bucketName,
Key: version.Key,
VersionId: version.VersionId,
...(BypassGovernanceRetention && {
BypassGovernanceRetention,
}),
})
.promise();
}

for (const marker of listedObjects.DeleteMarkers) {
await this.s3
.deleteObject({
Bucket: bucketName,
Key: marker.Key,
VersionId: marker.VersionId,
...(BypassGovernanceRetention && {
BypassGovernanceRetention,
}),
})
.promise();
}
return this.s3.send(new ListObjectVersionsCommand(param))
.then(data => Promise.all(
(data.Versions || [])
.filter(object => !object.Key.endsWith('/'))
.map(object =>
this.s3.send(new DeleteObjectCommand({
Bucket: bucketName,
Key: object.Key,
VersionId: object.VersionId,
...(BypassGovernanceRetention && { BypassGovernanceRetention }),
})).then(() => object)
)
.concat((data.Versions || [])
.filter(object => object.Key.endsWith('/'))
.map(object =>
this.s3.send(new DeleteObjectCommand({
Bucket: bucketName,
Key: object.Key,
VersionId: object.VersionId,
...(BypassGovernanceRetention && { BypassGovernanceRetention }),
}))
.then(() => object)
)
)
.concat((data.DeleteMarkers || [])
.map(object =>
this.s3.send(new DeleteObjectCommand({
Bucket: bucketName,
Key: object.Key,
VersionId: object.VersionId,
...(BypassGovernanceRetention && { BypassGovernanceRetention }),
}))
.then(() => object)
)
)
)
);
}

emptyMany(bucketNames) {
const promises = bucketNames.map(bucketName => this.empty(bucketName));

const promises = bucketNames.map(
bucketName => this.empty(bucketName)
);
return Promise.all(promises);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

emptyIfExists(bucketName) {
return this.bucketExists(bucketName).then(exists => {
if (exists) {
Expand All @@ -154,20 +149,19 @@ class BucketUtility {
return undefined;
});
}

emptyManyIfExists(bucketNames) {
const promises = bucketNames.map(bucketName =>
this.emptyIfExists(bucketName),
);

return Promise.all(promises);
}

getOwner() {
return this.s3
.listBuckets()
.promise()
.then(data => data.Owner);
return this.s3.send(new ListBucketsCommand({}))
.then(data => data.Owner)
.catch(err => {
throw err;
});
}
}

Expand Down
25 changes: 12 additions & 13 deletions tests/functional/aws-node-sdk/test/support/awsConfig.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const AWS = require('aws-sdk');
const { fromIni } = require('@aws-sdk/credential-providers');
const fs = require('fs');
const path = require('path');
const { config } = require('../../../../../lib/Config');
const https = require('https');
const http = require('http');

function getAwsCredentials(profile, credFile) {
const filename = path.join(process.env.HOME, credFile);

Expand All @@ -14,7 +15,7 @@ function getAwsCredentials(profile, credFile) {
throw new Error(msg);
}

return new AWS.SharedIniFileCredentials({ profile, filename });
return fromIni({ profile, filepath: filename });
}

function getRealAwsConfig(location) {
Expand All @@ -26,19 +27,18 @@ function getRealAwsConfig(location) {
const params = {
endpoint: gcpEndpoint ?
`${proto}://${gcpEndpoint}` : `${proto}://${awsEndpoint}`,
signatureVersion: 'v4',
};
if (config.locationConstraints[location].type === 'gcp') {
params.mainBucket = bucketName;
params.mpuBucket = mpuBucketName;
}
if (useHTTPS) {
params.httpOptions = {
agent: new https.Agent({ keepAlive: true }),
params.requestHandler = {
httpsAgent: new https.Agent({ keepAlive: true }),
};
} else {
params.httpOptions = {
agent: new http.Agent({ keepAlive: true }),
params.requestHandler = {
httpAgent: new http.Agent({ keepAlive: true }),
};
}
if (credentialsProfile) {
Expand All @@ -48,13 +48,12 @@ function getRealAwsConfig(location) {
return params;
}
if (pathStyle) {
params.s3ForcePathStyle = true;
}
if (!useHTTPS) {
params.sslEnabled = false;
params.forcePathStyle = true;
}
params.accessKeyId = locCredentials.accessKey;
params.secretAccessKey = locCredentials.secretKey;
params.credentials = {
accessKeyId: locCredentials.accessKey,
secretAccessKey: locCredentials.secretKey,
};
return params;
}

Expand Down
Loading
Loading