#
Set up the S3 client
>
import boto3
storage_client = boto3.client('s3', endpoint_url='https://s3-object-storage.kloudbeansite.com')
#
Fetch and display the list of available buckets
>
buckets_info = storage_client.list_buckets()
for storage_bucket in buckets_info['Buckets']:
print(f'Bucket: {storage_bucket["Name"]}')
#
Retrieve and display the objects within a specific bucket
>
objects_in_bucket = storage_client.list_objects_v2(Bucket='kloud-storage-bucket')
for storage_object in objects_in_bucket.get('Contents', []):
print(f'Object: {storage_object["Key"]}')
#
Upload a file to the specified bucket
>
upload_status = storage_client.upload_file('example.txt', 'kloud-storage-bucket', 'example.txt')
#
Download a file from the specified bucket
>
download_status = storage_client.download_file('kloud-storage-bucket', 'example.txt', 'downloaded_example.txt')
#
Import required packages
>
import { S3Client } from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
import fs from "fs";
#
Create a client for S3 endpoint
>
const kloudClient = new S3Client({
region: "auto",
endpoint: "https://s3-object-storage.kloudbeansite.com",
});
#
Create a file stream for uploading
>
const fileStream = fs.createReadStream("example.dmg");
#
Upload the file to KloudBean bucket
>
(async () => {
const kloudUpload = new Upload({
params: {
Bucket: "kloud-storage-bucket",
Key: "example-100.dmg",
Body: fileStream,
},
client: kloudClient,
});
// Wait until the upload is done
await kloudUpload.done();
})();
#
Package declaration and imports
>
package main
import (
"context"
"log"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
#
Main function implementation
>
func main() {
// Load AWS SDK configuration
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Printf("Unable to load configuration. Error: %v\n", err)
return
}
// Create a new S3 client for KloudBean
cloudStorageClient := s3.NewFromConfig(cfg, func(o *s3.Options) {
o.BaseEndpoint = aws.String("https://s3-object-storage.kloudbeansite.com")
})
// Open the file to upload
fileToUpload, err := os.Open("document.txt")
if err != nil {
log.Printf("Unable to open file for upload. Error: %v\n", err)
return
} else {
defer fileToUpload.Close()
// Upload the file to the KloudBean bucket
_, uploadErr := cloudStorageClient.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String("kloud-files-bucket"),
Key: aws.String("document-1.txt"),
Body: fileToUpload,
})
if uploadErr != nil {
log.Printf("Unable to upload the file. Error: %v\n", uploadErr)
} else {
log.Println("File uploaded successfully!")
}
}
}
#
Import AWS SDK and set up variables
>
require "aws-sdk"
storage_bucket = "kloud-storage-bucket"
#
Initialize S3 client for KloudBean
>
cloud_s3 = Aws::S3::Client.new(
region: "auto",
endpoint: "https://s3-object-storage.kloudbeansite.com",
)
#
List the first ten objects in the KloudBean bucket
>
response = cloud_s3.list_objects(bucket: 'kloud-storage-bucket', max_keys: 10)
response.contents.each do |obj|
puts "#{obj.key} => #{obj.etag}"
end
#
Upload a file to the KloudBean bucket
>
uploaded_file_name = "example-file-#{Time.now.to_i}"
begin
cloud_s3.put_object(
bucket: storage_bucket,
key: uploaded_file_name,
body: File.read("example.txt")
)
puts "Uploaded #{uploaded_file_name} to #{storage_bucket}."
rescue Exception => e
puts "Failed to upload #{uploaded_file_name} with error: #{e.message}"
exit "Please fix error with file upload before continuing."
end
#
Import required packages
>
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;
import java.nio.file.Paths;
import java.net.URI;
#
Class implementation with main method
>
public class KloudBeanS3Example {
public static void main(String[] args) {
String bucket = "kloud-storage-bucket";
String fileToUpload = "example.txt";
String uploadedFileName = "uploaded-example-" + System.currentTimeMillis() + ".txt";
S3Client s3 = S3Client.builder()
.region(Region.US_EAST_1)
.endpointOverride(URI.create("https://s3-object-storage.kloudbeansite.com"))
.credentialsProvider(DefaultCredentialsProvider.create())
.build();
try {
// List first 10 objects in the bucket
s3.listObjectsV2(ListObjectsV2Request.builder().bucket(bucket).maxKeys(10).build())
.contents().forEach(obj -> System.out.println(obj.key() + " => " + obj.eTag()));
// Upload file
s3.putObject(PutObjectRequest.builder().bucket(bucket).key(uploadedFileName).build(),
Paths.get(fileToUpload));
System.out.println("Uploaded " + uploadedFileName);
} catch (S3Exception e) {
System.err.println("Error: " + e.awsErrorDetails().errorMessage());
} finally {
s3.close();
}
}
}
#
Import AWS SDK and set up variables
>
#
Create an S3 client with KloudBean endpoint
>
$s3Client = new S3Client([
'region' => 'auto',
'version' => 'latest',
'endpoint' => 'https://s3-object-storage.kloudbeansite.com',
]);
#
List objects and upload a file
>
try {
// List the first 10 objects in the bucket
$result = $s3Client->listObjectsV2([
'Bucket' => $bucketName,
'MaxKeys' => 10
]);
foreach ($result['Contents'] as $object) {
echo $object['Key'] . ' => ' . $object['ETag'] . PHP_EOL;
}
// Upload a file
$s3Client->putObject([
'Bucket' => $bucketName,
'Key' => $uploadedFileName,
'Body' => fopen($filePath, 'r')
]);
echo "Uploaded $uploadedFileName to $bucketName." . PHP_EOL;
} catch (AwsException $e) {
echo "Error: " . $e->getMessage() . PHP_EOL;
}
?>