• 沒有找到結果。

Run the example

在文檔中 AWS SDK for JavaScript (頁 23-26)

console.log("Error", err);

} // Create an object and upload it to the Amazon S3 bucket.

try {

const results = await s3Client.send(new PutObjectCommand(params));

console.log(

"Successfully created " + params.Key +

" and uploaded it to " + params.Bucket +

"/" + params.Key );

return results; // For unit tests.

} catch (err) {

console.log("Error", err);

}};

run();

The example code can be found here on GitHub.

Step 3: Run the example

Enter the following command to run the example.

node sample.js

If the upload is successful, you'll see a confirmation message at the command prompt. You can also find the bucket and the uploaded text object in the Amazon S3 console.

Getting started in React Native

This tutorial shows you how you can create a React Native app using React Native CLI.

This tutorial shows you:

• How to install and include the AWS SDK for JavaScript version 3 (V3) modules that your project uses.

• How to write code that connects to Amazon Simple Storage Service (Amazon S3) to create and delete an Amazon S3 bucket.

The Scenario

Amazon S3 is a cloud service that enables you to store and retrieve any amount of data at any time, from anywhere on the web. React Native is a development framework that enables you to create mobile applications. This tutorial shows you how you can create a React Native app that connects to Amazon S3 to create and delete an Amazon S3 bucket.

The app uses the following SDK for JavaScript APIs:

Setup for this tutorial

• CognitoIdentityClient constructor

• S3 constructor

Setup for this tutorial

This section provides the minimal setup needed to complete this tutorial. You shouldn't consider this to be a full setup. For that, see Setting up the SDK for JavaScript (p. 23).

NoteIf you've already completed any of the following steps through other tutorials or existing configuration, skip those steps.

Create an AWS account

To create an AWS account, see How do I create and activate a new Amazon Web Services account?

Create AWS credentials and a profile

To perform these tutorials, you need to create an AWS Identity and Access Management (IAM) user and obtain credentials for that user. After you have those credentials, you make them available to the SDK in your development environment. Here's how.

To create and use credentials

1. Sign in to the AWS Management Console and open the IAM console at https://

console.aws.amazon.com/iam/.

2. Choose Users, and then choose Add user.

3. Provide a user name. For this tutorial, we'll use React-Native-Tutorial-User.

4. Under Select AWS access type, select Programmatic access, and then choose Next: Permissions.

5. Choose Attach existing policies directly.

6. In Search, enter s3, and then select AmazonS3FullAccess.

7. Choose Next: Tags, Next: Review, and Create user.

8. Record the credentials for React-Native-Tutorial-User. You can do so by downloading the .csv file or by copying and pasting the Access key ID and Secret access key.

Warning

Use appropriate security measures to keep these credentials safe and rotated.

9. Create or open the shared AWS credentials file. This file is ~/.aws/credentials on Linux and macOS systems, and %USERPROFILE%\.aws\credentials on Windows.

10. Add the following text to the shared AWS credentials file, but replace the example ID and example key with the ones you obtained earlier. Remember to save the file.

[javascript-tutorials]

aws_access_key_id = AKIAIOSFODNN7EXAMPLE

aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

The preceding procedure is the simplest of several possibilities for authentication and authorization. For complete information, see Setting credentials (p. 31).

Install other tools

To complete this tutorial, you need to set up your React Native development environment.

Step 1: Create an Amazon Cognito Identity Pool

You also need to install the following tools:

• Node.js

• Xcode if you're testing on IOS.

• Android Studio if you're testing on Android.

Step 1: Create an Amazon Cognito Identity Pool

In this exercise, you create and use an Amazon Cognito Identity pool to provide unauthenticated access to your app for the Amazon S3 service. Creating an identity pool also creates two AWS Identity and Access Management (IAM) roles, one to support users authenticated by an identity provider and the other to support unauthenticated guest users.

In this exercise, we will only work with the unauthenticated user role to keep the task focused. You can integrate support for an identity provider and authenticated users later.

To create an Amazon Cognito Identity pool

1. Sign in to the AWS Management Console and open the Amazon Cognito console at Amazon Web Services Console.

2. Choose Manage Identity Pools on the console opening page.

3. On the next page, choose Create new identity pool.

NoteIf there are no other identity pools, the Amazon Cognito console will skip this page and open the next page instead.

4. In the Getting started wizard, type a name for your identity pool in Identity pool name.

5. Choose Enable access to unauthenticated identities.

6. Choose Create Pool.

7. On the next page, choose View Details to see the names of the two IAM roles created for your identity pool. Make a note of the name of the role for unauthenticated identities. You need this name to add the required policy for Amazon S3.

8. Choose Allow.

9. On the Sample code page, select the Platform of JavaScript. Then, copy or write down the identity pool ID and the Region. You need these values to replace REGION and IDENTITY_POOL_ID in your browser script.

After you create your Amazon Cognito identity pool, you're ready to add permissions for Amazon S3 that are needed by your React Native app.

Step 2: Add a Policy to the Created IAM Role

To enable browser script access to Amazon S3 to create and delete an Amazon S3 bucket, use the unauthenticated IAM role created for your Amazon Cognito identity pool. This requires you to add an IAM policy to the role. For more information about IAM roles, see Creating a Role to Delegate Permissions to an AWS Service in the IAM User Guide.

To add an Amazon S3 policy to the IAM role associated with unauthenticated users 1. Sign in to the AWS Management Console and open the IAM console at https://

console.aws.amazon.com/iam/.

2. In the navigation panel on the left of the page, choose Roles.

在文檔中 AWS SDK for JavaScript (頁 23-26)

相關文件