-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy paths3.ts
222 lines (200 loc) · 4.62 KB
/
s3.ts
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import S3 = require('../clients/s3');
import fs = require('fs');
import {Endpoint} from '../lib/endpoint';
// Instantiate S3 without options
var s3 = new S3();
// Instantiate S3 without using the global credentials
new S3({
credentials: null
});
// Instantiate S3 with an Endpoint object
new S3({
endpoint: new Endpoint('awsproxy.example.com')
});
// test waiters
s3.waitFor('bucketExists', {
Bucket: 'test',
$waiter: {
maxAttempts: 100,
delay: 30
}
});
// Instantiate S3 with config
var s3Config: S3.Types.ClientConfiguration = {
apiVersion: '2006-03-01',
region: 'us-west-2',
params: {
Bucket: 'BUCKET'
},
signatureVersion: 'v4'
};
s3 = new S3(s3Config);
/**
* Call operation with:
* * params
* * callback
*/
s3.listObjects({
Bucket: 'BUCKET2'
}, function(err, data) {
if (err) {
console.log(err.extendedRequestId);
} else if (data && data.Contents) {
data.Contents.forEach(function(content) {
console.log(content.Key);
});
}
});
/**
* Call operation with:
* * no params
* * callback
*/
s3.listObjects(function(err, data) {
if (err) {
console.log(err.code);
} else if (data && data.Contents) {
data.Contents.forEach(function(content) {
console.log(content.Key);
});
}
});
/**
* Call operation with:
* * no params
* * no callback
*/
var listObjectsReq = s3.listObjects();
// Test success/error event listeners
listObjectsReq.on('error', function(err, resp) {
console.log(err.code);
console.log(resp.error);
});
listObjectsReq.on('success', function(resp) {
if (resp.data && resp.data.Contents) {
resp.data.Contents.forEach(function(content) {
console.log(content.Key);
});
}
});
// test complete event listener
listObjectsReq.on('complete', function(resp) {
if (resp.error) {
console.log(resp.error.code);
} else if (resp.data && resp.data.Contents) {
resp.data.Contents.forEach(function(content) {
console.log(content.Key);
});
}
});
// test send
listObjectsReq.send(function(err, data) {
if (err) {
console.log(err.code);
} else if (data && data.Contents) {
data.Contents.forEach(function(content) {
console.log(content.Key);
});
}
});
// test promise support
listObjectsReq.promise().then(function(data) {
data.Contents = data.Contents || [];
data.Contents.forEach(function(content) {
console.log(content.Key);
});
}).catch(function(err) {
console.log(err);
});
// test stream
var s3GetObjectStream = s3.getObject({
Bucket: 'BUCKET',
Key: 'KEY'
}).createReadStream();
// test putObject
s3.putObject({
Bucket: 'BUCKET',
Key: 'Test',
Body: 'text'
});
s3.putObject({
Bucket: 'BUCKET',
Key: 'Test',
Body: new Buffer('text')
});
s3.putObject({
Bucket: 'BUCKET',
Key: 'Test',
Body: fs.createReadStream('/fake/path')
});
const printUrl = (url: string)=> {
console.log(url);
}
s3.getSignedUrlPromise('getObject', {
Bucket: 'bucket',
Key: 'key'
}).then(printUrl);
const upload = s3.upload(
{
Bucket: 'BUCKET',
Key: 'KEY',
Body: new Buffer('some data')
},
{
tags: [
{
Key: 'key',
Value: 'value',
},
{
Key: 'otherKey',
Value: 'otherValue',
},
],
}
);
// test managed upload promise support
upload.promise()
.then((data) => data.Location);
const uploader = new S3.ManagedUpload({
queueSize: 4
});
uploader.on('httpUploadProgress', function(progress) {
console.log(progress.loaded);
console.log(progress.total);
});
uploader.send(function(err, data) {
if (err) {
console.log(err.code);
} else if (data) {
console.log(data.Bucket);
}
});
uploader.abort();
uploader.promise().then(function(data) {
console.log(data.Bucket);
});
var uploader2: S3.ManagedUpload = new S3.ManagedUpload(<S3.ManagedUpload.ManagedUploadOptions>{
service: s3,
params: {
Bucket: 'bucket',
Key: 'key',
Body: 'test'
}
});
uploader2.send((err, data) => {
});
var endpoint: Endpoint = s3.endpoint;
const presignedPost: S3.Types.PresignedPost = s3.createPresignedPost({
Bucket: 'bucket',
Conditions: [
['starts-with', '$key', ''],
['content-length-range', 0, 1024 * 1024 * 1024],
],
Fields: {
'acl': 'bucket-owner-full-control'
},
Expires: 900
});
const url: string = presignedPost.url;
const fields: S3.Types.PresignedPost.Fields = presignedPost.fields;