Certainly! If your Cloud Object Storage is S3-compatible, you can interact with it using the AWS S3 REST API. To do this without any external libraries, you’ll need to:
- Manually sign your requests using AWS Signature Version 4.
- Send HTTP requests using the built-in
https or fetch (in Node.js 18+ or browsers).
Below is a Node.js example (using only built-in modules) that lists all buckets.
Note: This is a simplified version for educational purposes. Production code should handle errors, edge cases, and security more robustly.
1. Fill in your credentials and endpoint
const crypto = require('crypto');
const https = require('https');
// ==== CONFIGURATION ====
const accessKeyId = 'YOUR_ACCESS_KEY';
const secretAccessKey = 'YOUR_SECRET_KEY';
const region = 'us-standard'; // or your region
const service = 's3';
const endpoint = 's3.your-cloud.com'; // e.g., s3.us.cloud-object-storage.appdomain.cloud
// =======================
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;
}
function hash(string) {
return crypto.createHash('sha256').update(string, 'utf8').digest('hex');
}
function hmac(key, string) {
return crypto.createHmac('sha256', key).update(string, 'utf8').digest('hex');
}
function getAmzDate(date) {
return date.toISOString().replace(/[:-]|\.\d{3}/g, '');
}
function getDateStamp(date) {
return date.toISOString().slice(0, 10).replace(/-/g, '');
}
function listBuckets() {
const method = 'GET';
const canonicalUri = '/';
const canonicalQuerystring = '';
const host = endpoint;
const now = new Date();
const amzDate = getAmzDate(now); // YYYYMMDD'T'HHMMSS'Z'
const dateStamp = getDateStamp(now); // YYYYMMDD
// ************* TASK 1: CREATE CANONICAL REQUEST *************
const canonicalHeaders = `host:${host}\nx-amz-date:${amzDate}\n`;
const signedHeaders = 'host;x-amz-date';
const payloadHash = hash(''); // GET request, so empty payload
const canonicalRequest = [
method,
canonicalUri,
canonicalQuerystring,
canonicalHeaders,
signedHeaders,
payloadHash
].join('\n');
// ************* TASK 2: CREATE STRING TO SIGN *************
const algorithm = 'AWS4-HMAC-SHA256';
const credentialScope = `${dateStamp}/${region}/${service}/aws4_request`;
const stringToSign = [
algorithm,
amzDate,
credentialScope,
hash(canonicalRequest)
].join('\n');
// ************* TASK 3: CALCULATE THE SIGNATURE *************
const signingKey = getSignatureKey(secretAccessKey, dateStamp, region, service);
const signature = hmac(signingKey, stringToSign);
// ************* TASK 4: ADD SIGNING INFORMATION TO THE REQUEST *************
const authorizationHeader = [
`${algorithm} Credential=${accessKeyId}/${credentialScope}`,
`SignedHeaders=${signedHeaders}`,
`Signature=${signature}`
].join(', ');
const options = {
hostname: host,
port: 443,
path: '/',
method: method,
headers: {
'x-amz-date': amzDate,
'Authorization': authorizationHeader,
'Host': host
}
};
// ************* TASK 5: SEND THE REQUEST *************
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => { data += chunk; });
res.on('end', () => {
if (res.statusCode === 200) {
// Parse XML response
const bucketNames = [...data.matchAll(/<Name>([^<]+)<\/Name>/g)].map(m => m[1]);
console.log('Buckets:', bucketNames);
} else {
console.error('Error:', res.statusCode, data);
}
});
});
req.on('error', (e) => {
console.error('Request error:', e);
});
req.end();
}
listBuckets();
How it works:
- Manually signs the request using AWS Signature V4.
- Sends a GET request to the S3 endpoint.
- Parses the XML response to extract bucket names.
Replace the following with your values:
YOUR_ACCESS_KEY
YOUR_SECRET_KEY
s3.your-cloud.com (your S3-compatible endpoint)
region (if your provider uses a different region string)
Note:
- This code is for Node.js (no external libraries).
- For browser JS, you’d use
fetch and the Web Crypto API for HMAC/SHA256, but the logic is similar.
Let me know if you need a browser version or further explanation!