Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Esempi di Amazon S3 che utilizzano SDK for PHP
Gli esempi di codice seguenti mostrano come eseguire azioni e implementare scenari comuni utilizzando ilAWS SDK for PHPcon Amazon S3.
Operazionisono estratti di codice che mostrano come eseguire chiamate alle singole funzioni Amazon S3.
Scenarisono esempi di codice che mostrano come eseguire un'attività specifica richiamando più funzioni Amazon S3.
Ogni esempio include un collegamento a GitHub, dove è possibile trovare le istruzioni su come configurare ed eseguire il codice nel contesto.
Argomenti
Operazioni
L'esempio di codice seguente mostra come copiare un oggetto Amazon S3 da un bucket a un altro.
- SDK for PHP
-
Copia semplice di un oggetto.
$s3client = new Aws\S3\S3Client(['region' => 'us-west-2', 'version' => 'latest']); try { $folder = "copied-folder"; $s3client->copyObject([ 'Bucket' => $bucket_name, 'CopySource' => "$bucket_name/$file_name", 'Key' => "$folder/$file_name-copy", ]); echo "Copied $file_name to $folder/$file_name-copy.\n"; } catch (Exception $exception) { echo "Failed to copy $file_name with error: " . $exception->getMessage(); exit("Please fix error with object copying before continuing."); }
-
Trova le istruzioni e altro codice su GitHub
. -
Per informazioni dettagliate, consulta CopyObject nella Documentazione di riferimento delle API AWS SDK for PHP.
-
L'esempio di codice seguente mostra come creare un bucket Amazon S3.
- SDK for PHP
-
Crea un bucket.
$s3client = new Aws\S3\S3Client(['region' => 'us-west-2', 'version' => 'latest']); try { $s3client->createBucket([ 'Bucket' => $bucket_name, 'CreateBucketConfiguration' => ['LocationConstraint' => $region], ]); echo "Created bucket named: $bucket_name \n"; } catch (Exception $exception) { echo "Failed to create bucket $bucket_name with error: " . $exception->getMessage(); exit("Please fix error with bucket creation before continuing."); }
-
Trova le istruzioni e altro codice su GitHub
. -
Per ulteriori informazioni, consulta CreateBucket nella Documentazione di riferimento delle API AWS SDK for PHP.
-
L'esempio di codice seguente mostra come eliminare un bucket Amazon S3 vuoto.
- SDK for PHP
-
Elimina un bucket vuoto.
$s3client = new Aws\S3\S3Client(['region' => 'us-west-2', 'version' => 'latest']); try { $s3client->deleteBucket([ 'Bucket' => $bucket_name, ]); echo "Deleted bucket $bucket_name.\n"; } catch (Exception $exception) { echo "Failed to delete $bucket_name with error: " . $exception->getMessage(); exit("Please fix error with bucket deletion before continuing."); }
-
Trova le istruzioni e altro codice su GitHub
. -
Per informazioni dettagliate sulle API, consulta DeleteBucket nella Documentazione di riferimento delle API AWS SDK for PHP.
-
L'esempio di codice seguente mostra come eliminare più oggetti da un bucket Amazon S3.
- SDK for PHP
-
Elimina un set di oggetti da un elenco di chiavi.
$s3client = new Aws\S3\S3Client(['region' => 'us-west-2', 'version' => 'latest']); try { $objects = []; foreach ($contents['Contents'] as $content) { $objects[] = [ 'Key' => $content['Key'], ]; } $s3client->deleteObjects([ 'Bucket' => $bucket_name, 'Key' => $file_name, 'Delete' => [ 'Objects' => $objects, ], ]); $check = $s3client->listObjects([ 'Bucket' => $bucket_name, ]); if (count($check) <= 0) { throw new Exception("Bucket wasn't empty."); } echo "Deleted all objects and folders from $bucket_name.\n"; } catch (Exception $exception) { echo "Failed to delete $file_name from $bucket_name with error: " . $exception->getMessage(); exit("Please fix error with object deletion before continuing."); }
-
Trova le istruzioni e altro codice su GitHub
. -
Per informazioni dettagliate sulle API, consulta DeleteObjects nella Documentazione di riferimento delle API AWS SDK for PHP.
-
L'esempio di codice seguente mostra come leggere i dati da un oggetto in un bucket Amazon S3.
- SDK for PHP
-
Recupera un oggetto.
$s3client = new Aws\S3\S3Client(['region' => 'us-west-2', 'version' => 'latest']); try { $file = $s3client->getObject([ 'Bucket' => $bucket_name, 'Key' => $file_name, ]); $body = $file->get('Body'); $body->rewind(); echo "Downloaded the file and it begins with: {$body->read(26)}.\n"; } catch (Exception $exception) { echo "Failed to download $file_name from $bucket_name with error: " . $exception->getMessage(); exit("Please fix error with file downloading before continuing."); }
L'esempio di codice seguente mostra come elencare gli oggetti in un bucket Amazon S3.
- SDK for PHP
-
Elenca gli oggetti in un bucket.
$s3client = new Aws\S3\S3Client(['region' => 'us-west-2', 'version' => 'latest']); try { $contents = $s3client->listObjects([ 'Bucket' => $bucket_name, ]); echo "The contents of your bucket are: \n"; foreach ($contents['Contents'] as $content) { echo $content['Key'] . "\n"; } } catch (Exception $exception) { echo "Failed to list objects in $bucket_name with error: " . $exception->getMessage(); exit("Please fix error with listing objects before continuing."); }
-
Trova le istruzioni e altro codice su GitHub
. -
Per informazioni dettagliate sulle API, consulta ListObjects nella Documentazione di riferimento delle API AWS SDK for PHP.
-
L'esempio di codice seguente mostra come caricare un oggetto in un bucket Amazon S3.
- SDK for PHP
-
Carica un oggetto in un bucket.
$s3client = new Aws\S3\S3Client(['region' => 'us-west-2', 'version' => 'latest']); $file_name = "local-file-" . uniqid(); try { $s3client->putObject([ 'Bucket' => $bucket_name, 'Key' => $file_name, 'SourceFile' => 'testfile.txt' ]); echo "Uploaded $file_name to $bucket_name.\n"; } catch (Exception $exception) { echo "Failed to upload $file_name with error: " . $exception->getMessage(); exit("Please fix error with file upload before continuing."); }
Scenari
L'esempio di codice seguente mostra come:
Creare un bucket.
Caricare un file nel bucket.
Scaricare un oggetto da un bucket.
Copiare un oggetto in una sottocartella in un bucket.
Elencare gli oggetti in un bucket.
Eliminare gli oggetti in un bucket.
Eliminare un bucket.
- SDK for PHP
-
require 'vendor/autoload.php'; use Aws\S3\S3Client; echo("--------------------------------------\n"); print("Welcome to the Amazon S3 getting started demo using PHP!\n"); echo("--------------------------------------\n"); $region = 'us-west-2'; $version = 'latest'; $s3client = new S3Client([ 'region' => $region, 'version' => $version ]); /* Inline declaration example $s3client = new Aws\S3\S3Client(['region' => 'us-west-2', 'version' => 'latest']); */ $bucket_name = "doc-example-bucket-" . uniqid(); try { $s3client->createBucket([ 'Bucket' => $bucket_name, 'CreateBucketConfiguration' => ['LocationConstraint' => $region], ]); echo "Created bucket named: $bucket_name \n"; } catch (Exception $exception) { echo "Failed to create bucket $bucket_name with error: " . $exception->getMessage(); exit("Please fix error with bucket creation before continuing."); } $file_name = "local-file-" . uniqid(); try { $s3client->putObject([ 'Bucket' => $bucket_name, 'Key' => $file_name, 'SourceFile' => 'testfile.txt' ]); echo "Uploaded $file_name to $bucket_name.\n"; } catch (Exception $exception) { echo "Failed to upload $file_name with error: " . $exception->getMessage(); exit("Please fix error with file upload before continuing."); } try { $file = $s3client->getObject([ 'Bucket' => $bucket_name, 'Key' => $file_name, ]); $body = $file->get('Body'); $body->rewind(); echo "Downloaded the file and it begins with: {$body->read(26)}.\n"; } catch (Exception $exception) { echo "Failed to download $file_name from $bucket_name with error: " . $exception->getMessage(); exit("Please fix error with file downloading before continuing."); } try { $folder = "copied-folder"; $s3client->copyObject([ 'Bucket' => $bucket_name, 'CopySource' => "$bucket_name/$file_name", 'Key' => "$folder/$file_name-copy", ]); echo "Copied $file_name to $folder/$file_name-copy.\n"; } catch (Exception $exception) { echo "Failed to copy $file_name with error: " . $exception->getMessage(); exit("Please fix error with object copying before continuing."); } try { $contents = $s3client->listObjects([ 'Bucket' => $bucket_name, ]); echo "The contents of your bucket are: \n"; foreach ($contents['Contents'] as $content) { echo $content['Key'] . "\n"; } } catch (Exception $exception) { echo "Failed to list objects in $bucket_name with error: " . $exception->getMessage(); exit("Please fix error with listing objects before continuing."); } try { $objects = []; foreach ($contents['Contents'] as $content) { $objects[] = [ 'Key' => $content['Key'], ]; } $s3client->deleteObjects([ 'Bucket' => $bucket_name, 'Key' => $file_name, 'Delete' => [ 'Objects' => $objects, ], ]); $check = $s3client->listObjects([ 'Bucket' => $bucket_name, ]); if (count($check) <= 0) { throw new Exception("Bucket wasn't empty."); } echo "Deleted all objects and folders from $bucket_name.\n"; } catch (Exception $exception) { echo "Failed to delete $file_name from $bucket_name with error: " . $exception->getMessage(); exit("Please fix error with object deletion before continuing."); } try { $s3client->deleteBucket([ 'Bucket' => $bucket_name, ]); echo "Deleted bucket $bucket_name.\n"; } catch (Exception $exception) { echo "Failed to delete $bucket_name with error: " . $exception->getMessage(); exit("Please fix error with bucket deletion before continuing."); } echo "Successfully ran the Amazon S3 with PHP demo.\n";
-
Trova istruzioni e altro codice su GitHub
. -
Per informazioni dettagliate sulle API, consulta i seguenti argomenti nella Documentazione di riferimento delle API AWS SDK for PHP.
-