Skip to content

Commit 732e84a

Browse files
authored
Merge pull request #356 from oracle-devrel/add-os-replication-example-function-repo
os replication example function repo added
2 parents 85535cc + 9703f69 commit 732e84a

File tree

6 files changed

+188
-0
lines changed

6 files changed

+188
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!--
2+
Copyright (c) 2021 Oracle and/or its affiliates.
3+
4+
The Universal Permissive License (UPL), Version 1.0
5+
6+
Subject to the condition set forth below, permission is hereby granted to any
7+
person obtaining a copy of this software, associated documentation and/or data
8+
(collectively the "Software"), free of charge and under any and all copyright
9+
rights in the Software, and any and all patent rights owned or freely
10+
licensable by each licensor hereunder covering either (i) the unmodified
11+
Software as contributed to or provided by such licensor, or (ii) the Larger
12+
Works (as defined below), to deal in both
13+
14+
(a) the Software, and
15+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
one is included with the Software (each a "Larger Work" to which the Software
17+
is contributed by such licensors),
18+
19+
without restriction, including without limitation the rights to copy, create
20+
derivative works of, display, perform, and distribute the Software and make,
21+
use, sell, offer for sale, import, export, have made, and have sold the
22+
Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
either these or other terms.
24+
25+
This license is subject to the following condition:
26+
The above copyright notice and either this complete permission notice or at
27+
a minimum a reference to the UPL must be included in all copies or
28+
substantial portions of the Software.
29+
30+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
SOFTWARE.
37+
-->
38+
39+
## Functions overview
40+
[Document functions-overview](files/Fn.pdf)
41+
42+
# Example function to copy a file from an Object Storage bucket to multiple other buckets when uploaded
43+
44+
### The Use case
45+
46+
The use case is that this function will be triggered by the upload CloudEvent of the source bucket
47+
and then it copies the received file to multiple (1-n) other buckets that are replicated to other regions
48+
using the Object Storage buckets automatic replication feature. This way we can implement content distribution
49+
in Object Storage buckets across multiple regions.
50+
51+
<p>
52+
<h3>Function configuration</h3>
53+
<img src="images/config.png" width="800">
54+
<ul>
55+
<li><code>TENANCY</code> is the tenancy os namespace name e.g. what you get when running <code>oci os ns get</code></li>
56+
<li><code>SOURCE_BUCKET</code> is the bucket where the file is uploaded and triggers this function using a CloudEvent</li>
57+
<li><code>TARGET_BUCKETS</code> are the comma delimited buckets to where the file is copied from the source bucket are replicatyed to other regions using the Object Storage replication feature</li>
58+
</ul>
59+
60+
<h3>Function example log when triggered by the Object Storage file upload (create/update) CloudEvent</h3>
61+
<img src="images/log.png" width="800">
62+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Copyright (c) 2021 Oracle and/or its affiliates.
3+
4+
The Universal Permissive License (UPL), Version 1.0
5+
6+
Subject to the condition set forth below, permission is hereby granted to any
7+
person obtaining a copy of this software, associated documentation and/or data
8+
(collectively the "Software"), free of charge and under any and all copyright
9+
rights in the Software, and any and all patent rights owned or freely
10+
licensable by each licensor hereunder covering either (i) the unmodified
11+
Software as contributed to or provided by such licensor, or (ii) the Larger
12+
Works (as defined below), to deal in both
13+
14+
(a) the Software, and
15+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
one is included with the Software (each a "Larger Work" to which the Software
17+
is contributed by such licensors),
18+
19+
without restriction, including without limitation the rights to copy, create
20+
derivative works of, display, perform, and distribute the Software and make,
21+
use, sell, offer for sale, import, export, have made, and have sold the
22+
Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
either these or other terms.
24+
25+
This license is subject to the following condition:
26+
The above copyright notice and either this complete permission notice or at
27+
a minimum a reference to the UPL must be included in all copies or
28+
substantial portions of the Software.
29+
30+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
SOFTWARE.
37+
*/
38+
39+
const fdk=require('@fnproject/fdk');
40+
const core = require("oci-core");
41+
const common = require("oci-common");
42+
const os = require("oci-objectstorage");
43+
44+
fdk.handle(async function(event){
45+
//console.log(event);
46+
var res = "ok";
47+
const provider = common.ResourcePrincipalAuthenticationDetailsProvider.builder();
48+
const client = new os.ObjectStorageClient({
49+
authenticationDetailsProvider: provider
50+
});
51+
try {
52+
console.log("Running os replication .. ");
53+
if(event.data && event.data.resourceName)
54+
{
55+
var file = event.data.resourceName;
56+
console.log(file);
57+
var ns = process.env.NAMESPACE;
58+
var sourceBucketName = process.env.SOURCE_BUCKET;
59+
var targetBucketNames = process.env.TARGET_BUCKETS;
60+
console.log("Reading " + file + " from " + sourceBucketName);
61+
var getObjectRequest = {
62+
namespaceName: ns,
63+
bucketName: sourceBucketName,
64+
objectName: file
65+
};
66+
var getObjectResponse = await client.getObject(getObjectRequest).catch(error => {
67+
console.log(error);
68+
res = error;
69+
});
70+
if(res == "ok") {
71+
console.log(getObjectResponse.value._readableState.buffer.head.data);
72+
var chunks = [];
73+
for await (let chunk of getObjectResponse.value) {
74+
chunks.push(chunk);
75+
}
76+
var buffer = Buffer.concat(chunks);
77+
//console.log(buffer);
78+
var targetbucketName = targetBucketNames.split(",");
79+
for (var i = 0; i < targetbucketName.length; i++)
80+
{
81+
console.log("Writing " + file + " to " + targetbucketName[i]);
82+
var putObjectRequest = {
83+
namespaceName: ns,
84+
bucketName: targetbucketName[i],
85+
putObjectBody: buffer,
86+
objectName: file,
87+
contentLength: buffer.length
88+
};
89+
var putObjectResponse = await client.putObject(putObjectRequest).catch(error => {
90+
console.log(error);
91+
res = error;
92+
i = targetbucketName[i].length;
93+
});
94+
}
95+
}
96+
}
97+
} catch (error) {
98+
console.log("Error: " + error);
99+
res = "Replication error " + error;
100+
} finally {
101+
return res;
102+
}
103+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
schema_version: 20180708
2+
name: os-replication
3+
version: 0.0.10
4+
runtime: node
5+
build_image: fnproject/node:14-dev
6+
run_image: fnproject/node:14
7+
entrypoint: node func.js
8+
284 KB
Loading
526 KB
Loading
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "os-replication",
3+
"version": "1.0.0",
4+
"description": "os replication example function",
5+
"main": "func.js",
6+
"author": "",
7+
"license": "Apache-2.0",
8+
"dependencies": {
9+
"@fnproject/fdk": ">=0.0.56",
10+
"oci-core": "^2.52.0",
11+
"oci-common": "^2.52.0",
12+
"oci-objectstorage": "^2.52.0"
13+
}
14+
}
15+

0 commit comments

Comments
 (0)