PHP용 AWS SDK를 사용하여 객체 업로드
이 단원에서는 PHP용 AWS SDK의 클래스를 사용하여 최대 5GB 크기의 객체를 업로드하는 방법을 설명합니다. 5GB보다 더 큰 파일은 멀티파트 업로드 API를 사용해야 합니다. 자세한 내용은 멀티파트 업로드 API를 사용한 객체 업로드를 참조하십시오.
참고
이미 PHP용 AWS SDK 사용 및 PHP 예제 실행의 지침에 따라 PHP용 AWS SDK가 올바르게 설치되어 있다고 가정합니다.
객체 업로드
1 |
Aws\S3\S3Client 클래스의 factory() 메서드를 사용하여 Amazon S3 클라이언트의 인스턴스를 만듭니다. |
2 |
Aws\S3\S3Client::putObject() 메서드를 실행합니다. 파일을 업로드할 경우 |
다음 PHP 코드 예제는 putObject
메서드의 어레이 파라미터인 SourceFile
키에 지정된 파일을 업로드하여 객체를 생성하는 방법을 보여줍니다.
use Aws\S3\S3Client; $bucket = '
*** Your Bucket Name ***
'; $keyname = '*** Your Object Key ***
'; // $filepath should be absolute path to a file on disk $filepath = '*** Your File Path ***
'; // Instantiate the client. $s3 = S3Client::factory(); // Upload a file. $result = $s3->putObject(array( 'Bucket' => $bucket, 'Key' => $keyname, 'SourceFile' => $filepath, 'ContentType' => 'text/plain', 'ACL' => 'public-read', 'StorageClass' => 'REDUCED_REDUNDANCY', 'Metadata' => array( 'param1' => 'value 1', 'param2' => 'value 2' ) )); echo $result['ObjectURL'];
파일 이름을 지정하는 대신, 다음 PHP 코드 예제에서와 같이 Body
키로 어레이 파라미터를 지정하여 인라인으로 객체 데이터를 제공할 수 있습니다.
use Aws\S3\S3Client; $bucket = '
*** Your Bucket Name ***
'; $keyname = '*** Your Object Key ***
'; // Instantiate the client. $s3 = S3Client::factory(); // Upload data. $result = $s3->putObject(array( 'Bucket' => $bucket, 'Key' => $keyname, 'Body' => 'Hello, world!' )); echo $result['ObjectURL'];
예 데이터를 업로드하여 Amazon S3 버킷에 객체 생성
다음 PHP 예제는 putObject()
메서드를 사용한 데이터 업로드를 통해 지정된 버킷에 객체를 생성합니다. 이 가이드의 PHP 예제 실행에 대한 자세한 내용은 PHP 예제 실행 단원을 참조하십시오.
<?php // Include the AWS SDK using the Composer autoloader. require 'vendor/autoload.php'; use Aws\S3\S3Client; use Aws\S3\Exception\S3Exception; $bucket = '*** Your Bucket Name ***'; $keyname = '*** Your Object Key ***'; // Instantiate the client. $s3 = S3Client::factory(); try { // Upload data. $result = $s3->putObject(array( 'Bucket' => $bucket, 'Key' => $keyname, 'Body' => 'Hello, world!', 'ACL' => 'public-read' )); // Print the URL to the object. echo $result['ObjectURL'] . "\n"; } catch (S3Exception $e) { echo $e->getMessage() . "\n"; }