بارگذاری آبجکت داخل صندوقچه
هنگام آپلود فایل در صندوقچه اگر اندازهی آبجکت بیش از 400 مگابایت باشد، باید به جای بارگذاری شی در یک عملیات، از بارگذاری چند بخشی (Multipart Upload) استفاده کنید.
مولفهها
- نام صندوقچه
- نام آبجکت
- مسیر فایل
نام آبجکت (Key)
نام آبجکت پس از بارگذاری داخل صندوقچه
مسیر فایل (SourceFile)
مسیری که فایل باید بارگذاری شود
- NET.
- PHP
- Python
- Javascript
- GO
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache - 2.0
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading.Tasks;
using System.Reflection;
namespace UploadObject
{
// The following example shows two different methods for uploading data to
// an Amazon Simple Storage Service (Amazon S3) bucket. The method,
// UploadObjectFromFileAsync, uploads an existing file to an Amazon S3
// bucket. The method, UploadObjectFromContentAsync, creates a new
// file containing the text supplied to the method. The application
// was created using AWS SDK for .NET 3.5 and .NET 5.0.
class UploadObject
{
private static IAmazonS3 _s3Client;
private const string BUCKET_NAME = "<BUCKET_NAME>";
private const string OBJECT_NAME = "<OBJECT_NAME>";
private static string LOCAL_PATH = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
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);
// The method expects the full path, including the file name.
var path = $"{LOCAL_PATH}/{OBJECT_NAME}";
await UploadObjectFromFileAsync(_s3Client, BUCKET_NAME, OBJECT_NAME, path);
}
/// <summary>
/// This method uploads a file to an Amazon S3 bucket. This
/// example method also adds metadata for the uploaded file.
/// </summary>
/// <param name="client">An initialized Amazon S3 client object.</param>
/// <param name="bucketName">The name of the S3 bucket to upload the
/// file to.</param>
/// <param name="objectName">The destination file name.</param>
/// <param name="filePath">The full path, including file name, to the
/// file to upload. This doesn't necessarily have to be the same as the
/// name of the destination file.</param>
private static async Task UploadObjectFromFileAsync(
IAmazonS3 client,
string bucketName,
string objectName,
string filePath)
{
try
{
var putRequest = new PutObjectRequest
{
BucketName = bucketName,
Key = objectName,
FilePath = filePath,
ContentType = "text/plain"
};
putRequest.Metadata.Add("x-amz-meta-title", "someTitle");
PutObjectResponse response = await client.PutObjectAsync(putRequest);
foreach (PropertyInfo prop in response.GetType().GetProperties())
{
Console.WriteLine($"{prop.Name}: {prop.GetValue(response, null)}");
}
Console.WriteLine($"Object {OBJECT_NAME} added to {bucketName} bucket");
}
catch (AmazonS3Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}
}
<?php
require('client.php');
try {
$result = $client->putObject([
'Bucket' => $config['sample_bucket'],
'Key' => 'file-uploaded-from-php-sdk.png',
'SourceFile' => '../files/file.png',
]);
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
import boto3
import logging
from botocore.exceptions import ClientError
# Configure logging
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 = s3_resource.Bucket('bucket-name')
file_path = 'the/abs/path/to/file.txt'
object_name = 'file.txt'
with open(file_path, "rb") as file:
bucket.put_object(
ACL='private',
Body=file,
Key=object_name
)
except ClientError as e:
logging.error(e)
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');
const fs = require('fs');
const path = require('path');
// Create an S3 client service object
const s3 = new S3Client({
region: 'default',
endpoint: 'endpoint_url',
credentials: {
accessKeyId: 'access_key',
secretAccessKey: 'secret_key',
},
});
const uploadParams = {
Bucket: 'sample_bucket', // bucket name
Key: 'object-name', // the name of the selected file
ACL: 'public-read', // 'private' | 'public-read'
Body: 'BODY',
};
// BODY (the contents of the uploaded file - leave blank/remove to retain contents of original file.)
const file = 'file.png'; //FILE_NAME (the name of the file to upload (if you don't specify KEY))
// call S3 to retrieve upload file to specified bucket
const run = async () => {
// Configure the file stream and obtain the upload parameters
const fileStream = fs.createReadStream(file);
fileStream.on('error', function (err) {
console.log('File Error', err);
});
uploadParams.Key = path.basename(file);
// call S3 to upload file to specified bucket
uploadParams.Body = fileStream;
try {
const data = await s3.send(new PutObjectCommand(uploadParams));
console.log('Success', data);
} 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/s3manager"
)
// Creates a S3 Bucket in the region configured in the shared config
// or AWS_REGION environment variable.
//
// Usage:
// go run s3_upload_object.go BUCKET_NAME FILENAME
func main() {
if len(os.Args) != 3 {
exitErrorf("bucket and file name required\nUsage: %s bucket_name filename",
os.Args[0])
}
bucket := os.Args[1]
filename := os.Args[2]
file, err := os.Open(filename)
if err != nil {
exitErrorf("Unable to open file %q, %v", err)
}
defer file.Close()
// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("<ACCESS_KEY>", "<SECRET_KEY>", ""),
Region: aws.String("default"),
Endpoint: aws.String("<ENDPOINT_URL>"),
})
// Setup the S3 Upload Manager. Also see the SDK doc for the Upload Manager
// for more information on configuring part size, and concurrency.
//
// http://docs.aws.amazon.com/sdk-for-go/api/service/s3/s3manager/#NewUploader
uploader := s3manager.NewUploader(sess)
// Upload the file's body to S3 bucket as an object with the key being the
// same as the filename.
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
// Can also use the `filepath` standard library package to modify the
// filename as need for an S3 object key. Such as turning absolute path
// to a relative path.
Key: aws.String(filename),
// The file to be uploaded. io.ReadSeeker is preferred as the Uploader
// will be able to optimize memory when uploading large content. io.Reader
// is supported, but will require buffering of the reader's bytes for
// each part.
Body: file,
})
if err != nil {
// Print the error and exit.
exitErrorf("Unable to upload %q to %q, %v", filename, bucket, err)
}
fmt.Printf("Successfully uploaded %q to %q\n", filename, bucket)
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
برای اجرای قطعه کد بالا با فرض نامگذاری فایل کد به s3_upload_object.go میتوان از دستور زیر استفاده کرد:
go run s3_upload_object.go BUCKET_NAME FILENAME