-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathApp.tsx
117 lines (106 loc) · 3.24 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[javascript.v3.reactnative.GettingStarted]
import React, { useCallback, useState } from "react";
import { Button, StyleSheet, Text, TextInput, View } from "react-native";
import "react-native-get-random-values";
import "react-native-url-polyfill/auto";
import {
S3Client,
CreateBucketCommand,
DeleteBucketCommand,
} from "@aws-sdk/client-s3";
import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers";
const client = new S3Client({
// The AWS Region where the Amazon Simple Storage Service (Amazon S3) bucket will be created. Replace this with your Region.
region: "us-east-1",
credentials: fromCognitoIdentityPool({
// Replace the value of 'identityPoolId' with the ID of an Amazon Cognito identity pool in your Amazon Cognito Region.
identityPoolId: "us-east-1:edbe2c04-7f5d-469b-85e5-98096bd75492",
// Replace the value of 'region' with your Amazon Cognito Region.
clientConfig: { region: "us-east-1" },
}),
});
enum MessageType {
SUCCESS = 0,
FAILURE = 1,
EMPTY = 2,
}
const App = () => {
const [bucketName, setBucketName] = useState("");
const [msg, setMsg] = useState<{ message: string; type: MessageType }>({
message: "",
type: MessageType.EMPTY,
});
const createBucket = useCallback(async () => {
setMsg({ message: "", type: MessageType.EMPTY });
try {
await client.send(new CreateBucketCommand({ Bucket: bucketName }));
setMsg({
message: `Bucket "${bucketName}" created.`,
type: MessageType.SUCCESS,
});
} catch (e) {
console.error(e);
setMsg({
message: e instanceof Error ? e.message : "Unknown error",
type: MessageType.FAILURE,
});
}
}, [bucketName]);
const deleteBucket = useCallback(async () => {
setMsg({ message: "", type: MessageType.EMPTY });
try {
await client.send(new DeleteBucketCommand({ Bucket: bucketName }));
setMsg({
message: `Bucket "${bucketName}" deleted.`,
type: MessageType.SUCCESS,
});
} catch (e) {
setMsg({
message: e instanceof Error ? e.message : "Unknown error",
type: MessageType.FAILURE,
});
}
}, [bucketName]);
return (
<View style={styles.container}>
{msg.type !== MessageType.EMPTY && (
<Text
style={
msg.type === MessageType.SUCCESS
? styles.successText
: styles.failureText
}
>
{msg.message}
</Text>
)}
<View>
<TextInput
onChangeText={(text) => setBucketName(text)}
autoCapitalize={"none"}
value={bucketName}
placeholder={"Enter Bucket Name"}
/>
<Button color="#68a0cf" title="Create Bucket" onPress={createBucket} />
<Button color="#68a0cf" title="Delete Bucket" onPress={deleteBucket} />
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
successText: {
color: "green",
},
failureText: {
color: "red",
},
});
export default App;
// snippet-end:[javascript.v3.reactnative.GettingStarted]