View a markdown version of this page

Menggunakan Bucket Amazon S3 sebagai Host Web Statis - AWS SDK untuk JavaScript

AWS SDK untuk JavaScript V2 telah mencapai akhir dukungan. Kami menyarankan Anda bermigrasi ke AWS SDK untuk JavaScript v3. Untuk detail dan informasi tambahan tentang cara bermigrasi, silakan lihat pengumuman ini.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Menggunakan Bucket Amazon S3 sebagai Host Web Statis

JavaScript code example that applies to Node.js execution

Contoh Node.js kode ini menunjukkan:

  • Cara mengatur bucket Amazon S3 sebagai host web statis.

Skenario

Dalam contoh ini, serangkaian Node.js modul digunakan untuk mengkonfigurasi bucket Anda untuk bertindak sebagai host web statis. Node.js Modul menggunakan SDK untuk JavaScript mengonfigurasi bucket Amazon S3 yang dipilih menggunakan metode kelas klien Amazon S3 berikut:

Untuk informasi selengkapnya tentang menggunakan bucket Amazon S3 sebagai host web statis, lihat Hosting Situs Web Statis di Amazon S3 di Panduan Pengguna Layanan Penyimpanan Sederhana Amazon.

Tugas Prasyarat

Untuk mengatur dan menjalankan contoh ini, Anda harus menyelesaikan tugas ini terlebih dahulu:

Mengkonfigurasi SDK

Konfigurasikan SDK untuk JavaScript dengan membuat objek konfigurasi global lalu mengatur Wilayah untuk kode Anda. Dalam contoh ini, Wilayah diatur keus-west-2.

// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Set the Region AWS.config.update({region: 'us-west-2'});

Mengambil Konfigurasi Situs Web Bucket Saat Ini

Buat Node.js modul dengan nama files3_getbucketwebsite.js. Modul ini mengambil argumen baris perintah tunggal yang menentukan bucket yang konfigurasi situs webnya Anda inginkan. Konfigurasikan SDK seperti yang ditunjukkan sebelumnya.

Buat objek AWS.S3 layanan. Buat fungsi yang mengambil konfigurasi situs web bucket saat ini untuk bucket yang dipilih dalam bucket list. Satu-satunya parameter yang perlu Anda lewati adalah nama bucket yang dipilih saat memanggil getBucketWebsite metode. Jika bucket saat ini memiliki konfigurasi situs web, konfigurasi tersebut dikembalikan oleh Amazon S3 dalam data parameter yang diteruskan ke fungsi callback.

Jika bucket yang dipilih tidak memiliki konfigurasi situs web, informasi tersebut dikembalikan ke fungsi callback di err parameter.

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create S3 service object s3 = new AWS.S3({ apiVersion: "2006-03-01" }); var bucketParams = { Bucket: process.argv[2] }; // call S3 to retrieve the website configuration for selected bucket s3.getBucketWebsite(bucketParams, function (err, data) { if (err) { console.log("Error", err); } else if (data) { console.log("Success", data); } });

Untuk menjalankan contoh, ketik berikut ini di baris perintah.

node s3_getbucketwebsite.js BUCKET_NAME

Kode sampel ini dapat ditemukan di sini GitHub.

Mengatur Konfigurasi Situs Web Bucket

Buat Node.js modul dengan nama files3_setbucketwebsite.js. Pastikan untuk mengkonfigurasi SDK seperti yang ditunjukkan sebelumnya. Buat objek AWS.S3 layanan.

Buat fungsi yang menerapkan konfigurasi situs web bucket. Konfigurasi memungkinkan bucket yang dipilih berfungsi sebagai host web statis. Konfigurasi situs web ditentukan dalam JSON. Pertama, buat objek JSON yang berisi semua nilai untuk menentukan konfigurasi situs web, kecuali untuk Key nilai yang mengidentifikasi dokumen kesalahan, dan Suffix nilai yang mengidentifikasi dokumen indeks.

Masukkan nilai elemen input teks ke objek JSON. Siapkan parameter untuk putBucketWebsite metode, termasuk nama bucket dan konfigurasi situs web JSON.

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create S3 service object s3 = new AWS.S3({ apiVersion: "2006-03-01" }); // Create JSON for putBucketWebsite parameters var staticHostParams = { Bucket: "", WebsiteConfiguration: { ErrorDocument: { Key: "", }, IndexDocument: { Suffix: "", }, }, }; // Insert specified bucket name and index and error documents into params JSON // from command line arguments staticHostParams.Bucket = process.argv[2]; staticHostParams.WebsiteConfiguration.IndexDocument.Suffix = process.argv[3]; staticHostParams.WebsiteConfiguration.ErrorDocument.Key = process.argv[4]; // set the new website configuration on the selected bucket s3.putBucketWebsite(staticHostParams, function (err, data) { if (err) { // display error message console.log("Error", err); } else { // update the displayed website configuration for the selected bucket console.log("Success", data); } });

Untuk menjalankan contoh, ketik berikut ini di baris perintah.

node s3_setbucketwebsite.js BUCKET_NAME INDEX_PAGE ERROR_PAGE

Kode sampel ini dapat ditemukan di sini GitHub.

Menghapus Konfigurasi Situs Web Bucket

Buat Node.js modul dengan nama files3_deletebucketwebsite.js. Pastikan untuk mengkonfigurasi SDK seperti yang ditunjukkan sebelumnya. Buat objek AWS.S3 layanan.

Buat fungsi yang menghapus konfigurasi situs web untuk bucket yang dipilih. Satu-satunya parameter yang perlu Anda lewati saat memanggil deleteBucketWebsite metode adalah nama bucket yang dipilih.

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create S3 service object s3 = new AWS.S3({ apiVersion: "2006-03-01" }); var bucketParams = { Bucket: process.argv[2] }; // call S3 to delete website configuration for selected bucket s3.deleteBucketWebsite(bucketParams, function (error, data) { if (error) { console.log("Error", err); } else if (data) { console.log("Success", data); } });

Untuk menjalankan contoh, ketik berikut ini di baris perintah.

node s3_deletebucketwebsite.js BUCKET_NAME

Kode sampel ini dapat ditemukan di sini GitHub.