Skip to content

Commit 2e4130d

Browse files
set up basic streamed upload for bluemix
1 parent 100607a commit 2e4130d

3 files changed

Lines changed: 1472 additions & 26 deletions

File tree

src/bluemix/config.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"project_id":"307972859230",
3-
"bucket_name":"image_api_307972859230",
2+
"bucket_name":"image-api",
3+
"public_endpoint":"s3.au-syd.cloud-object-storage.appdomain.cloud",
44
"keys": {
55
"path":"./keys",
66
"cloud_storage": {
7-
"path":"./../../keys/cloudStorage.json"
7+
"path":"./keys/ibm_storage.json"
88
},
99
"cloud_functions": {
10-
"path":"./../../keys/functions.json"
10+
"path":"./keys/ibm_functions.json"
1111
}
1212
}
1313
}

src/bluemix/experiments/upload.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,34 @@ const request = require('request');
77
const path = require('path');
88
const Storage = require('ibm-cos-sdk');
99

10-
const config = require('./../../keys/ibm_storage.json');
10+
const config = require('./../config.json');
11+
const key = require('./../keys/ibm_storage.json');
1112

12-
let storage = new Storage.S3(config);
13+
const s3Config = {
14+
endpoint: 's3.au-syd.cloud-object-storage.appdomain.cloud',
15+
apiKeyId: key.apikey,
16+
ibmAuthEndpoint: 'https://iam.ng.bluemix.net/oidc/token',
17+
serviceInstanceId: key.resource_instance_id,
18+
};
19+
20+
let storage = new Storage.S3(s3Config);
1321

1422
// Returns a Promise object which resolves to a stream.
1523
function uploadByURL(url, bucket, fileName) {
16-
//let file = bucket.file(fileName);
1724
return new Promise((resolve, reject) => {
1825
request(url)
1926
.on('response', (response) => { response.pause(); resolve(response); })
2027
.on('error', (reject));
2128
}).then((response) => {
22-
return storage.putObject({ Bucket:bucket, Key:fileName, Body:response })
29+
return storage.upload({ Bucket:bucket, Key:fileName, Body:response }) //putObject did not work for streams due to Sigv4
2330
.promise();
24-
})
31+
})
2532
.catch((error) => { return { error }; }); // TODO: Add more detailed error handling. - statusCode etc.
2633
}
2734

2835
exports.upload = function (url) {
29-
return uploadByURL(url, storage.bucket(config.bucket_name), path.basename(url));
36+
return uploadByURL(url, config.bucket_name, path.basename(url));
3037
};
31-
/*
32-
function createTextFile(bucketName, itemName, fileText) {
33-
console.log(`Creating new item: ${itemName}`);
34-
return cos.putObject({
35-
Bucket: bucketName,
36-
Key: itemName,
37-
Body: fileText
38-
}).promise()
39-
.then(() => {
40-
console.log(`Item: ${itemName} created!`);
41-
})
42-
.catch((e) => {
43-
console.error(`ERROR: ${e.code} - ${e.message}\n`);
44-
});
45-
}
46-
*/
4738

4839
/**
4940
*

0 commit comments

Comments
 (0)