To list all buckets in a Cloud Object Storage instance that is S3-compatible, and without using any external libraries (i.e., no aws-sdk or other third-party packages), you can implement the AWS Signature Version 4 (SigV4) protocol directly in Node.js using the built-in https and crypto modules.
✅ Key Requirements
- No external libraries (e.g.,
aws-sdk, axios, etc.)
- S3-compatible API (e.g., AWS S3, IBM COS, MinIO, etc.)
- Authentication via Access Key and Secret Key
- Use of the
GET / operation to list buckets
🧠 How It Works
The AWS SigV4 signing process involves:
- Date and Time formatting
- Credential scope creation
- Canonical request construction
- String to sign
- Signing key derivation
- Authorization header construction
- HTTP request to the S3 endpoint
🛠️ Code Implementation (Node.js)
const https = require('https');
const crypto = require('crypto');
// Replace with your actual credentials and endpoint
const accessKeyId = 'YOUR_ACCESS_KEY';
const secretAccessKey = 'YOUR_SECRET_KEY';
const region = 'us-east-1'; // e.g., 'us-east-1' for AWS S3
const service = 's3';
const endpoint = 's3.amazonaws.com'; // Replace with your S3-compatible endpoint
// Step 1: Get current date and time
const date = new Date();
const dateStamp = date.toISOString().slice(0, 10).replace(/-/g, ''); // YYYYMMDD
const requestDate = date.toISOString().replace(/.000Z/, 'Z'); // ISO8601 format
// Step 2: Create credential scope
const credentialScope = `${dateStamp}/${region}/${service}/aws4_request`;
// Step 3: Build canonical request
const httpMethod = 'GET';
const canonicalUri = '/';
const canonicalQueryString = '';
const canonicalHeaders = `host:${endpoint}\nx-amz-date:${requestDate}\n`;
const signedHeaders = 'host;x-amz-date';
const payloadHash = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'; // SHA256 of empty string
const canonicalRequest = [
httpMethod,
canonicalUri,
canonicalQueryString,
canonicalHeaders,
signedHeaders,
payloadHash
].join('\n');
// Step 4: Create string to sign
const algorithm = 'AWS4-HMAC-SHA256';
const stringToSign = [
algorithm,
requestDate,
credentialScope,
crypto.createHash('sha256').update(canonicalRequest).digest('hex')
].join('\n');
// Step 5: Derive signing key
function getSignatureKey(key, dateStamp, regionName, serviceName) {
const kDate = crypto.createHmac('sha256', 'AWS4' + key)
.update(dateStamp)
.digest();
const kRegion = crypto.createHmac('sha256', kDate)
.update(regionName)
.digest();
const kService = crypto.createHmac('sha256', kRegion)
.update(serviceName)
.digest();
const kSigning = crypto.createHmac('sha256', kService)
.update('aws4_request')
.digest();
return kSigning;
}
const signingKey = getSignatureKey(secretAccessKey, dateStamp, region, service);
// Step 6: Generate signature
const signature = crypto.createHmac('sha256', signingKey)
.update(stringToSign)
.digest('hex');
// Step 7: Construct Authorization header
const authorizationHeader = [
algorithm,
`Credential=${accessKeyId}/${credentialScope}`,
`SignedHeaders=${signedHeaders}`,
`Signature=${signature}`
].join(', ');
// Step 8: Make the HTTP request
const options = {
hostname: endpoint,
path: '/',
method: 'GET',
headers: {
'Host': endpoint,
'X-Amz-Date': requestDate,
'Authorization': authorizationHeader
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('Response:', data);
// Parse the XML response to extract bucket names
// You can use a DOM parser or a simple string parser
});
});
req.on('error', (e) => {
console.error('Error:', e.message);
});
req.end();
📝 Notes
- Replace the placeholders (
YOUR_ACCESS_KEY, YOUR_SECRET_KEY, region, endpoint) with your actual values.
- This script assumes the endpoint is a standard S3-compatible one (e.g.,
s3.amazonaws.com).
- Parsing the XML response is not included here, but you can use a DOM parser or a simple regex to extract bucket names from the XML.
- Security: Never hardcode credentials in production. Use environment variables or secure vaults.
🧪 Example Output (XML)
If successful, the response will be an XML document like:
<ListAllMyBucketsResult>
<Buckets>
<Bucket>
<Name>my-bucket-1</Name>
<CreationDate>2023-04-01T12:34:56.000Z</CreationDate>
</Bucket>
<Bucket>
<Name>my-bucket-2</Name>
<CreationDate>2023-04-02T09:12:34.000Z</CreationDate>
</Bucket>
</Buckets>
</ListAllMyBucketsResult>
⚠️ Limitations
- This implementation is Node.js-specific.
- It assumes the endpoint is HTTPS and S3-compatible.
- No XML parsing is included, but it's straightforward to add using
xmldom or DOMParser in the browser.
✅ Summary
This solution provides a fully self-contained way to list S3-compatible buckets using only Node.js core modules and manual AWS SigV4 signing, without relying on external libraries. It’s suitable for environments where you cannot use aws-sdk or other packages.