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

Kami mengumumkan yang akan datang end-of-support untuk AWS SDK for JavaScript v2. Kami menyarankan Anda bermigrasi ke AWS SDK for JavaScript v3. Untuk tanggal, detail tambahan, dan informasi tentang cara bermigrasi, silakan merujuk ke pengumuman tertaut.

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 kode Node.js ini menunjukkan:

  • Cara mengatur bucket Amazon S3 sebagai host web statis.

Skenario

Dalam contoh ini, serangkaian modul Node.js digunakan untuk mengonfigurasi bucket Anda untuk bertindak sebagai host web statis. Modul Node.js menggunakan SDK JavaScript untuk 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 terlebih dahulu menyelesaikan tugas-tugas ini:

Mengkonfigurasi SDK

Konfigurasikan SDK untuk JavaScript dengan membuat objek konfigurasi global lalu menyetel Wilayah untuk kode Anda. Dalam contoh ini, Region 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 modul Node.js dengan nama files3_getbucketwebsite.js. Modul ini mengambil satu argumen baris perintah 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 daftar keranjang. 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 dalam 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 contoh ini dapat ditemukan di sini GitHub.

Menyetel Konfigurasi Situs Web Bucket

Buat modul Node.js dengan nama files3_setbucketwebsite.js. Pastikan untuk mengonfigurasi 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 dalam objek JSON. Siapkan parameter untuk putBucketWebsite metode ini, 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 contoh ini dapat ditemukan di sini GitHub.

Menghapus Konfigurasi Situs Web Bucket

Buat modul Node.js dengan nama files3_deletebucketwebsite.js. Pastikan untuk mengonfigurasi 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 contoh ini dapat ditemukan di sini GitHub.