Javascript
1. Install The Dependencies
To install the project dependencies, simply run the following command in your terminal:
npm install
2. Setup The Environment
We will be setting up two environment variables, ACCESS_KEY
and SECRET_KEY
,
which are required to access the AIOZ W3S service. Assign the values of these
variables to the values of your credential that you created in the
AIOZ W3S dashboard (opens in a new tab).
export ACCESS_KEY=<your-access-key>
export SECRET_KEY=<your-secret-key>
3. List All Buckets
To list all buckets, you can use the following code:
node listBuckets.js
Other functionalities are straightforward and follow a similar pattern. You can check out the code example repository for a comprehensive understanding.
Minimal Code Example
import fs from 'fs'
import path from 'path'
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3'
// Create an Amazon S3 service client object.
const s3Client = new S3Client({
region: 'us-east-1',
credentials: {
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY'
},
endpoint: {
url: 'W3S_ENDPOINT_URL'
},
forcePathStyle: true
})
const filePath = 'YOUR_FILE_PATH'
// Set the bucket parameters
export const bucketParams = {
Bucket: 'YOUR_BUCKET_NAME',
Key: path.basename(filePath),
Body: fs.createReadStream(filePath)
}
export const run = async () => {
try {
const data = await s3Client.send(new PutObjectCommand(bucketParams))
console.log('Success', data)
} catch (err) {
console.log('Error', err)
}
}
run()