Utilizzare PutBucketWebsite con un AWS SDKo CLI - Amazon Simple Storage Service

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à.

Utilizzare PutBucketWebsite con un AWS SDKo CLI

I seguenti esempi di codice mostrano come utilizzarePutBucketWebsite.

.NET
AWS SDK for .NET
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

// Put the website configuration. PutBucketWebsiteRequest putRequest = new PutBucketWebsiteRequest() { BucketName = bucketName, WebsiteConfiguration = new WebsiteConfiguration() { IndexDocumentSuffix = indexDocumentSuffix, ErrorDocument = errorDocument, }, }; PutBucketWebsiteResponse response = await client.PutBucketWebsiteAsync(putRequest);
  • Per API i dettagli, vedere PutBucketWebsitein AWS SDK for .NET APIRiferimento.

C++
SDKper C++
Nota

C'è altro su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

bool AwsDoc::S3::putWebsiteConfig(const Aws::String &bucketName, const Aws::String &indexPage, const Aws::String &errorPage, const Aws::S3::S3ClientConfiguration &clientConfig) { Aws::S3::S3Client client(clientConfig); Aws::S3::Model::IndexDocument indexDocument; indexDocument.SetSuffix(indexPage); Aws::S3::Model::ErrorDocument errorDocument; errorDocument.SetKey(errorPage); Aws::S3::Model::WebsiteConfiguration websiteConfiguration; websiteConfiguration.SetIndexDocument(indexDocument); websiteConfiguration.SetErrorDocument(errorDocument); Aws::S3::Model::PutBucketWebsiteRequest request; request.SetBucket(bucketName); request.SetWebsiteConfiguration(websiteConfiguration); Aws::S3::Model::PutBucketWebsiteOutcome outcome = client.PutBucketWebsite(request); if (!outcome.IsSuccess()) { std::cerr << "Error: PutBucketWebsite: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Success: Set website configuration for bucket '" << bucketName << "'." << std::endl; } return outcome.IsSuccess(); }
CLI
AWS CLI

Applica una configurazione statica del sito Web a un bucket denominatomy-bucket:

aws s3api put-bucket-website --bucket my-bucket --website-configuration file://website.json

Il file website.json è un JSON documento nella cartella corrente che specifica l'indice e le pagine di errore per il sito Web:

{ "IndexDocument": { "Suffix": "index.html" }, "ErrorDocument": { "Key": "error.html" } }
Java
SDKper Java 2.x
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.IndexDocument; import software.amazon.awssdk.services.s3.model.PutBucketWebsiteRequest; import software.amazon.awssdk.services.s3.model.WebsiteConfiguration; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.regions.Region; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class SetWebsiteConfiguration { public static void main(String[] args) { final String usage = """ Usage: <bucketName> [indexdoc]\s Where: bucketName - The Amazon S3 bucket to set the website configuration on.\s indexdoc - The index document, ex. 'index.html' If not specified, 'index.html' will be set. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String bucketName = args[0]; String indexDoc = "index.html"; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .build(); setWebsiteConfig(s3, bucketName, indexDoc); s3.close(); } public static void setWebsiteConfig(S3Client s3, String bucketName, String indexDoc) { try { WebsiteConfiguration websiteConfig = WebsiteConfiguration.builder() .indexDocument(IndexDocument.builder().suffix(indexDoc).build()) .build(); PutBucketWebsiteRequest pubWebsiteReq = PutBucketWebsiteRequest.builder() .bucket(bucketName) .websiteConfiguration(websiteConfig) .build(); s3.putBucketWebsite(pubWebsiteReq); System.out.println("The call was successful"); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Per API i dettagli, vedere PutBucketWebsitein AWS SDK for Java 2.x APIRiferimento.

JavaScript
SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

Imposta la configurazione del sito Web.

import { PutBucketWebsiteCommand, S3Client } from "@aws-sdk/client-s3"; const client = new S3Client({}); // Set up a bucket as a static website. // The bucket needs to be publicly accessible. export const main = async () => { const command = new PutBucketWebsiteCommand({ Bucket: "test-bucket", WebsiteConfiguration: { ErrorDocument: { // The object key name to use when a 4XX class error occurs. Key: "error.html", }, IndexDocument: { // A suffix that is appended to a request that is for a directory. Suffix: "index.html", }, }, }); try { const response = await client.send(command); console.log(response); } catch (err) { console.error(err); } };
PowerShell
Strumenti per PowerShell

Esempio 1: il comando abilita l'hosting del sito Web per il bucket specificato con il documento indice come 'index.html' e il documento di errore come 'error.html'.

Write-S3BucketWebsite -BucketName 's3testbucket' -WebsiteConfiguration_IndexDocumentSuffix 'index.html' -WebsiteConfiguration_ErrorDocument 'error.html'
  • PutBucketWebsitePer i dettagli, vedere in API AWS Tools for PowerShell Riferimento al cmdlet.

Ruby
SDKper Ruby
Nota

c'è altro da fare. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

require "aws-sdk-s3" # Wraps Amazon S3 bucket website actions. class BucketWebsiteWrapper attr_reader :bucket_website # @param bucket_website [Aws::S3::BucketWebsite] A bucket website object configured with an existing bucket. def initialize(bucket_website) @bucket_website = bucket_website end # Sets a bucket as a static website. # # @param index_document [String] The name of the index document for the website. # @param error_document [String] The name of the error document to show for 4XX errors. # @return [Boolean] True when the bucket is configured as a website; otherwise, false. def set_website(index_document, error_document) @bucket_website.put( website_configuration: { index_document: { suffix: index_document }, error_document: { key: error_document } } ) true rescue Aws::Errors::ServiceError => e puts "Couldn't configure #{@bucket_website.bucket.name} as a website. Here's why: #{e.message}" false end end # Example usage: def run_demo bucket_name = "amzn-s3-demo-bucket" index_document = "index.html" error_document = "404.html" wrapper = BucketWebsiteWrapper.new(Aws::S3::BucketWebsite.new(bucket_name)) return unless wrapper.set_website(index_document, error_document) puts "Successfully configured bucket #{bucket_name} as a static website." end run_demo if $PROGRAM_NAME == __FILE__
  • Per API i dettagli, vedere PutBucketWebsitein AWS SDK for Ruby APIRiferimento.

Per un elenco completo di AWS SDKguide per sviluppatori ed esempi di codice, vediUtilizzo di questo servizio con un SDK AWS. Questo argomento include anche informazioni su come iniziare e dettagli sulle SDK versioni precedenti.