External Storage / IBM Cloud Object Storage
In order to use IBM Cloud Object Storage as your External Storage of choice, you must set the provider
attribute to ibm
within the store
hash and, at a minimum, provide your Azure Storage access key
as well as a bucket
name and IBM Cloud region
used. The full list of store
hash attibutes for IBM Cloud Object Storage can be found below.
store hash attributes for IBM Cloud Object Storage: | |||
Attribute | Type | Required | Description |
provider | String | Yes | provider must be set to ibm . |
key | String | Yes | Your IBM Cloud API Key. |
bucket | String | Yes | Name of a destination bucket in your IBM Cloud account. |
region | String | Yes | Name of the IBM Cloud region in which your bucket is located. |
path | String | No | Destination path in your IBM Cloud Object Storage bucket (without leading slash). Defaults to root. |
acl | String | No | The Access Control List of the destination object. This can be private , public-read , public-read-write , authenticated-read . Defaults to "public-read". |
{
"store": {
"provider": "ibm",
"key": "ibm-cloud-api-key",
"region": "ibm-cloud-region",
"bucket": "bucket-name",
"path": "file-path-in-bucket"
}
}
An example cURL request of using IBM Cloud Object Storage as the External Storage provider will look like the following:
curl https://api.optidash.ai/1.0/fetch -X POST -u your-api-key: \
-H "Content-Type: application/json" \
-d '{
"url": "https://www.website.com/image.jpg",
"resize": {
"width": 100,
"height": 75
},
"store": {
"provider": "ibm",
"key": "ibm-cloud-api-key",
"region": "eu-de",
"bucket": "images",
"path": "assets/image.jpg"
}
}'
When using IBM Cloud Object Storage as your External Storage, the url
property within the JSON response will point to the object's location within the IBM Cloud bucket and you can safely use that URL in production, for example:
HTTP/1.1 200 OK
Date:
Status: 200 OK
Content-Type: application/json
{
"success": true,
"code": 200,
"id": "0402d6d1-a94e-4554-b4d3-6fd6c6c09c66",
"input": {
"name": "image.jpg",
..
},
"output": {
"url": "https://s3.eu-de.cloud-object-storage.appdomain.cloud/images/assets/image.jpg",
..
}
}
Code Examples for IBM Cloud Object Storage
Below, you'll find examples of how to use IBM Cloud Object Storage from the Optidash API:
- PHP
- Node
- Ruby
- Go
- Python
- Java
<?php
/**
* Instantiate new `$opti` by calling a constructor
*/
$opti = new Optidash\Optidash('your-api-key');
/**
* Provide a publicly available image URL with fetch(string) method,
* resize the image to 100 x 75 and store it in IBM Cloud Object Storage
*/
$opti
->fetch('https://www.website.com/image.jpg')
->resize(array(
'width' => 100,
'height' => 75
))
->store(array(
'provider' => 'ibm',
'key' => 'ibm-cloud-api-key',
'region' => 'eu-de',
'bucket' => 'images',
'path' => 'assets/image.jpg'
))
->toJSON(function ($error, $meta) {
if (!empty($error)) {
throw new Exception($error);
}
/**
* You'll find the full JSON metadata array within the `$meta` variable.
* Remember to always check if the `success` property is set to `true`.
*/
if ($meta['success'] == true) {
print ($meta['output']['url']);
} else {
print ($meta['message']);
}
});
/**
* Module dependencies
*/
const Optidash = require("optidash");
/**
* Instantiate new `opti` by calling a constructor
*/
const opti = new Optidash("your-api-key");
/**
* Provide a publicly available image URL with fetch(string) method,
* resize the image to 100 x 75 and store it in IBM Cloud Object Storage
*/
opti
.fetch("https://www.website.com/image.jpg")
.resize({
width: 100,
height: 75
})
.store({
provider: "ibm",
key: "ibm-cloud-api-key",
region: "eu-de",
bucket: "images",
path: "assets/image.jpg"
})
.toJSON((err, meta) => {
if (err) {
return console.log(err);
}
/**
* You'll find the full JSON metadata hash within the `meta` variable.
* Remember to always check if the `success` property is set to `true`.
*/
if (meta.success) {
console.log(meta.output.url);
} else {
console.log(meta.message);
}
});
##
# Require dependencies
require "optidash"
##
# Instantiate new `opti` by calling a constructor
opti = Optidash.new("your-api-key")
##
# Provide a publicly available image URL with fetch(string) method,
# resize the image to 100 x 75 and store it in IBM Cloud Object Storage
err, meta = opti
.fetch("https://www.website.com/image.jpg")
.resize(
width: 100,
height: 75
)
.store(
provider: "ibm",
key: "ibm-cloud-api-key",
region: "eu-de",
bucket: "images",
path: "assets/image.jpg"
)
.to_json
abort(err) if err
##
# You'll find the full JSON metadata hash within the `meta` variable.
# Remember to always check if the `success` property is set to `true`.
if meta["success"]
puts meta["output"]["url"]
else
puts meta["message"]
end
package main
import (
"fmt"
"github.com/optidash/optidash-go"
)
func main() {
/**
* Instantiate new `opti` by calling a constructor
*/
opti, err := optidash.NewClient("your-api-key")
if err != nil {
panic(err)
}
/**
* Provide a publicly available image URL with Fetch(string) method,
* resize the image to 100 x 75 and store it in IBM Cloud Object Storage
*/
meta, err := opti.
Fetch("https://www.website.com/image.jpg").
Resize(optidash.P{
"width": 100,
"height": 75
}).
Store(optidash.P{
"provider": "ibm",
"key": "ibm-cloud-api-key",
"region": "eu-de",
"bucket": "images",
"path": "assets/image.jpg"
}).
ToJSON()
if err != nil {
panic(err)
}
/**
* You'll find the full JSON metadata hash within the `meta` variable.
* Remember to always check if the `success` property is set to `true`.
*/
if meta["success"].(map[string]interface{})["success"].(bool) {
fmt.Println(meta["output"].(map[string]interface{})["url"])
} else {
fmt.Println(meta["message"])
}
}
##
# Import dependencies
from optidash import optidash
##
# Instantiate new `opti` by calling a constructor
opti = optidash('your-api-key')
##
# Provide a publicly available image URL with fetch(string) method,
# resize the image to 100 x 75 and store it in IBM Cloud Object Storage
err, meta = (
opti
.fetch('https://www.website.com/image.jpg')
.resize({
'width': 100,
'height': 75
})
.store({
'provider': 'ibm',
'key': 'ibm-cloud-api-key',
'region': 'eu-de',
'container': 'images',
'path': 'assets/image.jpg'
})
.toJSON()
)
if err is not None:
raise StandardError(err)
##
# You'll find the full JSON metadata hash within the `meta` variable.
# Remember to always check if the `success` property is set to `true`.
if meta['success'] is True:
print (meta['output']['url'])
else:
print (meta['message'])
package com.optidash.examples;
/**
* Import dependencies
*/
import static com.optidash.OperationConfiguration.settings;
import com.optidash.Optidash;
import com.optidash.Fetch;
import com.optidash.Response;
public class StorageSoftlayer {
public static void main(String[] args) {
/**
* Instantiate new `opti` by calling a constructor
*/
Optidash opti = new Optidash("your-api-key");
/**
* Provide a publicly available image URL with fetch(string) method,
* resize the image to 100 x 75 and store it in IBM Cloud Object Storage
*/
final Response response = opti.fetch("https://www.website.com/image.jpg");
fetch.resize(
settings()
.set("width", 100)
.set("height", 75)
);
fetch.store(
settings()
.set("provider", "ibm")
.set("username", "ibm-cloud-username")
.set("region", "eu-de")
.set("container", "images")
.set("path", "assets/image.jpg")
);
final Response response = fetch.toJson();
/**
* You'll find the full JSON metadata hash within the `Response#getMetadata()` variable.
* Remember to always check if the `Response#successful` property is set to `true`.
*/
if (response.isSuccessful()) {
System.out.println(response.getMetadata().getOutput().get("url"));
} else {
System.out.println(response.getMessage());
}
}
}