

 AWS SDK untuk JavaScript V2 telah mencapai end-of-support. Kami menyarankan Anda bermigrasi ke [AWS SDK untuk JavaScript v3](https://docs.aws.amazon.com//sdk-for-javascript/v3/developer-guide/). Untuk detail dan informasi tambahan tentang cara bermigrasi, silakan lihat [pengumuman](https://aws.amazon.com/blogs//developer/announcing-end-of-support-for-aws-sdk-for-javascript-v2/) 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
<a name="s3-example-static-web-host"></a>

![JavaScript code example that applies to Node.js execution](http://docs.aws.amazon.com/id_id/sdk-for-javascript/v2/developer-guide/images/nodeicon.png)

**Contoh kode Node.js ini menunjukkan:**
+ Cara mengatur bucket Amazon S3 sebagai host web statis.

## Skenario
<a name="s3-example-static-web-host-scenario"></a>

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:
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getBucketWebsite-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getBucketWebsite-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putBucketWebsite-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putBucketWebsite-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteBucketWebsite-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteBucketWebsite-property)

Untuk informasi selengkapnya tentang menggunakan bucket Amazon S3 sebagai host web statis, lihat [Hosting Situs Web Statis di Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/WebsiteHosting.html) di Panduan Pengguna Layanan *Penyimpanan Sederhana Amazon*.

## Tugas Prasyarat
<a name="s3-example-static-web-host-prerequisites"></a>

Untuk mengatur dan menjalankan contoh ini, Anda harus terlebih dahulu menyelesaikan tugas-tugas ini:
+ Instal Node.js. Untuk informasi selengkapnya tentang menginstal Node.js, lihat [situs web Node.js](https://nodejs.org).
+ Buat file konfigurasi bersama dengan kredensi pengguna Anda. Untuk informasi selengkapnya tentang menyediakan file kredensial bersama, lihat. [Memuat Kredensial di Node.js dari File Kredensial Bersama](loading-node-credentials-shared.md)

## Mengkonfigurasi SDK
<a name="s3-example-static-web-host-configure-sdk"></a>

Konfigurasikan SDK untuk JavaScript dengan membuat objek konfigurasi global lalu menyetel Wilayah untuk kode Anda. Dalam contoh ini, Region diatur ke`us-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
<a name="s3-example-static-web-host-get-website"></a>

Buat modul Node.js dengan nama file`s3_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](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_getbucketwebsite.js).

## Menyetel Konfigurasi Situs Web Bucket
<a name="s3-example-static-web-host-set-website"></a>

Buat modul Node.js dengan nama file`s3_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](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_setbucketwebsite.js).

## Menghapus Konfigurasi Situs Web Bucket
<a name="s3-example-static-web-host-delete-website"></a>

Buat modul Node.js dengan nama file`s3_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](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_deletebucketwebsite.js).