Create Bucket
You must create a bucket before you can upload your files. The following code can be used to create one.
Components
- Credentials
- Bucket Name
- Bucket ACL
Bucket Name
Pay attention to the following details when naming the bucket:
- The bucket's name must be unique. The uniqueness of this name will be verified over the entire Arvancloud Object Storage.
- Special characters like the dot (. ), the hyphen (-), and the asterisk (!) should not be used in the bucket's name.
- Both capital and lowercase characters may be used to specify the bucket's name. However, after the bucket is created, the name will be changed to lowercase.
- Only English lettering should be used for the bucket's name.
- The bucket's name must have at least three letters.
Bucket ACL
The access level of the bucket defines how to access the information inside it.
- Private
- Public-read
- .NET
- PHP
- Python
- Javascript
- GO
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading.Tasks;
namespace CreateBucket
{
public class CreateBucket
{
// This example shows how to use Amazon Simple Storage Service (Amazon S3)
// to create a new Amazon S3 bucket. The examples uses AWS SDK for .NET 3.5 and
// .NET 5.0.
private static IAmazonS3 _s3Client;
// Specify the name of the new bucket.
private const string NEW_BUCKET_NAME = "<BUCKET_NAME>";
static async Task Main()
{
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("<ACCESS-KEY>", "<SECRET-KEY>");
var config = new AmazonS3Config { ServiceURL = "<ENDPOINT>" };
_s3Client = new AmazonS3Client(awsCredentials, config);
Console.WriteLine($"\nCreating a new bucket, named: {NEW_BUCKET_NAME}.");
await CreatingBucketAsync(_s3Client, NEW_BUCKET_NAME);
}
/// <summary>
/// Uses Amazon SDK for .NET PutBucketAsync to create a new
/// Amazon S3 bucket.
/// </summary>
/// <param name="client">The client object used to connect to Amazon S3.</param>
/// <param name="bucketName">The name of the bucket to create.</param>
static async Task CreatingBucketAsync(IAmazonS3 client, string bucketName)
{
try
{
var putBucketRequest = new PutBucketRequest
{
BucketName = bucketName,
UseClientRegion = true
};
var putBucketResponse = await client.PutBucketAsync(putBucketRequest);
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"Error creating bucket: '{ex.Message}'");
}
}
}
}
<?php
require('client.php');
function createBucket($s3Client, $bucketName)
{
try {
$result = $S3Client->createBucket([
'ACL' => 'private|public-read',
'Bucket' => $bucketName,
]);
return 'The bucket\'s location is: ' .
$result['Location'] . '. ' .
'The bucket\'s effective URI is: ' .
$result['@metadata']['effectiveUri'];
} catch (AwsException $e) {
return 'Error: ' . $e->getAwsErrorMessage();
}
}
echo createBucket($client, 'sample-bucket-4');
import boto3
import logging
from botocore.exceptions import ClientError
logging.basicConfig(level=logging.INFO)
try:
s3_resource = boto3.resource(
's3',
endpoint_url='endpoint_url',
aws_access_key_id='access_key',
aws_secret_access_key='secret_key'
)
except Exception as exc:
logging.error(exc)
else:
try:
bucket_name = 'sample-bucket_name'
bucket = s3_resource.Bucket(bucket_name)
bucket.create(ACL='public-read') # ACL='private'|'public-read'
except ClientError as exc:
logging.error(exc)
const { S3Client, CreateBucketCommand } = require('@aws-sdk/client-s3');
// Create an S3 client service object
const s3 = new S3Client({
region: 'default',
endpoint: 'endpoint_url',
credentials: {
accessKeyId: 'access_key',
secretAccessKey: 'secret_key',
},
});
const run = async () => {
try {
const data = await s3.send(
new CreateBucketCommand({
Bucket: 'sample-bucket',
ACL: 'public-read', // 'private' | 'public-read'
})
);
} catch (err) {
console.log('Error', err);
}
};
run();
package main
import (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
// Creates an S3 Bucket in the region configured in the shared config
// or AWS_REGION environment variable.
//
// Usage:
// go run s3_create_bucket BUCKET_NAME
func main() {
if len(os.Args) != 2 {
exitErrorf("Bucket name missing!\nUsage: %s bucket_name", os.Args[0])
}
bucket := os.Args[1]
sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("<ACCESS_KEY>", "<SECRET_KEY>", ""),
})
svc := s3.New(sess, &aws.Config{
Region: aws.String("default"),
Endpoint: aws.String("<ENDPOINT_URL>"),
})
// Create the S3 Bucket
_, err = svc.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
exitErrorf("Unable to create bucket %q, %v", bucket, err)
}
// Wait until bucket is created before finishing
fmt.Printf("Waiting for bucket %q to be created...\n", bucket)
err = svc.WaitUntilBucketExists(&s3.HeadBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
exitErrorf("Error occurred while waiting for bucket to be created, %v", bucket)
}
fmt.Printf("Bucket %q successfully created\n", bucket)
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
The following command can be used to execute the aforementioned code, presuming the code file is called s3_create_bucket.go:
go run s3_create_bucket.go BUCKET_NAME