@@ -63,44 +63,47 @@ You can define a default directory for uploads using the s3Config object
63
63
``` typescript
64
64
/* AWS S3 Client */
65
65
/* uploadFile.ts */
66
- import S3 from ' react-aws-s3-typescript' ;
66
+ import ReactS3Client from ' react-aws-s3-typescript' ;
67
67
import { s3Config } from ' ./s3Config.ts' ;
68
68
69
- /* Import s3 config object and call the constrcutor */
70
- const s3 = new S3 (s3Config );
71
-
72
- /* You can use the default directory defined in s3Config object
73
- * Or you can a define custom directory to upload when calling the
74
- * constructor using js/ts object destructuring.
75
- *
76
- * const s3 = new S3({
77
- * ...s3Config,
78
- * dirName: 'custom-directory'
79
- * });
80
- *
81
- */
82
-
83
- const filename = ' filename-to-be-uploaded' ; /* Optional */
84
-
85
- /* If you do not specify a file name, file will be uploaded using uuid generated
86
- * by short-UUID (https://www.npmjs.com/package/short-uuid)
87
- */
88
-
89
-
90
- s3 .uploadFile (file , filename ).then ((res ) => {
91
- console .log (res );
92
- /*
93
- * {
94
- * Response: {
95
- * bucket: "bucket-name",
96
- * key: "directory-name/filename-to-be-uploaded",
97
- * location: "https:/your-aws-s3-bucket-url/directory-name/filename-to-be-uploaded"
98
- * }
99
- * }
100
- */
101
- }).catch ((err ) => {
102
- console .log (err );
103
- })
69
+ const uploadFile = async () => {
70
+ /* Import s3 config object and call the constrcutor */
71
+ const s3 = new ReactS3Client (s3Config );
72
+
73
+ /* You can use the default directory defined in s3Config object
74
+ * Or you can a define custom directory to upload when calling the
75
+ * constructor using js/ts object destructuring.
76
+ *
77
+ * const s3 = new ReactS3Client({
78
+ * ...s3Config,
79
+ * dirName: 'custom-directory'
80
+ * });
81
+ *
82
+ */
83
+
84
+ const filename = ' filename-to-be-uploaded' ; /* Optional */
85
+
86
+ /* If you do not specify a file name, file will be uploaded using uuid generated
87
+ * by short-UUID (https://www.npmjs.com/package/short-uuid)
88
+ */
89
+
90
+ try {
91
+ const res = await s3 .uploadFile (file , filename );
92
+
93
+ console .log (res );
94
+ /*
95
+ * {
96
+ * Response: {
97
+ * bucket: "bucket-name",
98
+ * key: "directory-name/filename-to-be-uploaded",
99
+ * location: "https:/your-aws-s3-bucket-url/directory-name/filename-to-be-uploaded"
100
+ * }
101
+ * }
102
+ */
103
+ } catch (exception ) {
104
+ console .log (exception );
105
+ /* handle the exception */
106
+ }
104
107
105
108
/* End of uploadFile.ts */
106
109
` ` `
@@ -110,20 +113,25 @@ You can define a default directory for uploads using the s3Config object
110
113
` ` ` typescript
111
114
/* AWS S3 Client */
112
115
/* deleteFile.ts */
113
- import S3 from ' react-aws-s3-typescript' ;
116
+ import ReactS3Client from ' react-aws-s3-typescript' ;
114
117
import { s3Config } from ' ./s3Config.ts' ;
115
118
116
- /* Import s3 config object and call the constrcutor */
117
- const s3 = new S3 (s3Config );
119
+ const deleteFile = async () => {
120
+ /* Import s3 config object and call the constrcutor */
121
+ const s3 = new ReactS3Client (s3Config );
118
122
119
- /* Define the filepath from the root of the bucket to the file to be deleted */
120
- const filepath = ' directory-name/filename-to-be-deleted' ;
123
+ /* Define the filepath from the root of the bucket to the file to be deleted */
124
+ const filepath = ' directory-name/filename-to-be-deleted' ;
121
125
122
- await s3 .deleteFile (filepath ).then ((res ) => {
123
- console .log (' File deleted' );
124
- }).catch ((err ) => {
125
- console .log (err );
126
- })
126
+ try {
127
+ await s3 .deleteFile (filepath );
128
+
129
+ console .log (' File deleted' );
130
+ } catch (exception ) {
131
+ console .log (exception );
132
+ /* handle the exception */
133
+ }
134
+ }
127
135
128
136
/* End of deleteFile.ts */
129
137
` ` `
0 commit comments