API Reference and Developer Documentation
Google Cloud Storage (GCS) Integration
Kraken.io API allows you to store optimized images directly in your Google Cloud Storage (GCS) bucket. Follow the steps below to configure your GCS integration and securely store your optimized images.
Mandatory Parameters: | |
gcs_store.bucket | Name of the destination bucket on your Google Cloud Storage account. |
gcs_store.credentials | Your service account credentials (JSON format) to authenticate with GCS. |
Optional Parameters: | |
gcs_store.path | Destination path in your GCS bucket (e.g., "images/layout/header.jpg"). Defaults to root "/". |
gcs_store.acl | Permissions of the destination object. This can be "publicRead" or "private". Defaults to "private". |
gcs_store.metadata | Metadata you would like to assign to your GCS object (optimized image). |
Step 1: Creating a Google Cloud Storage Bucket
Log in to your Google Cloud Console. Navigate to the "Storage" section and select "Browser." Click "Create bucket" and follow the prompts. Note the bucket name for later use.
Step 2: Obtaining GCS Credentials
To interact with GCS through the API, you need appropriate credentials as a service account key:
- Go to "IAM & Admin" in Google Cloud Console.
- Select "Service Accounts" and click "Create Service Account."
- Enter a name and description for the service account.
- Assign necessary roles like "Storage Object Admin" or "Storage Object Creator."
- Click "Create Key" and choose JSON type. This will download a JSON file with your credentials.
Step 3: Configuring Your Kraken.io API Request
Include the necessary details in your Kraken.io API request to store the optimized image directly in your GCS bucket. Insert the JSON file's contents into the credentials
property inside gcs_store
. Example configuration:
{
"auth": {
"api_key": "your_api_key",
"api_secret": "your_api_secret"
},
"url": "https://example.com/image.png",
"lossy": true,
"wait": true,
"gcs_store": {
"acl": "private",
"bucket": "your-bucket-name",
"path": "path/to/your/image.png",
"metadata": {
"tag1": "value1",
"tag2": "value2"
},
"credentials": {
"type": "service_account",
"project_id": "your-project-id",
"private_key_id": "your-private-key-id",
"private_key": "your-private-key",
"client_email": "your-service-account-email",
"client_id": "your-client-id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "your-auth-provider-cert-url",
"client_x509_cert_url": "your-client-cert-url"
}
}
}
<?php
require_once("Kraken.php");
$kraken = new Kraken("your_api_key", "your_api_secret");
$params = array(
"url" => "https://example.com/image.png",
"wait" => true,
"lossy" => true,
"gcs_store" => array(
"acl" => "private",
"bucket" => "your-bucket-name",
"path" => "path/to/your/image.png",
"credentials" => json_decode(file_get_contents('path/to/your/credentials.json'), true)
)
);
$data = $kraken->url($params);
if ($data["success"]) {
echo "Success. Optimized image URL: " . $data["kraked_url"];
} else {
echo "Fail. Error message: " . $data["message"];
}
const fs = require('fs').promises;
const Kraken = require("kraken");
(async () => {
try {
const kraken = new Kraken({
api_key: "your_api_key",
api_secret: "your_api_secret"
});
const credentials = JSON.parse(await fs.readFile('path/to/your/credentials.json', 'utf8'));
const params = {
url: "https://example.com/image.png",
wait: true,
lossy: true,
gcs_store: {
acl: "private",
bucket: "your-bucket-name",
path: "path/to/your/image.png",
credentials
}
};
kraken.url(params, function (status) {
if (status.success) {
console.log("Success. Optimized image URL: %s", status.kraked_url);
} else {
console.log("Fail. Error message: %s", status.message);
}
});
} catch (error) {
console.error("Error:", error);
}
})();
require 'rubygems'
require 'kraken-io'
require 'json'
kraken = Kraken::API.new(
:api_key => 'your_api_key',
:api_secret => 'your_api_secret'
)
credentials = JSON.parse(File.read('path/to/your/credentials.json'))
params = {
:url => 'https://example.com/image.png',
:wait => true,
:lossy => true,
:gcs_store => {
'acl' => 'private',
'bucket' => 'your-bucket-name',
'path' => 'path/to/your/image.png',
'credentials' => credentials
}
}
data = kraken.url('https://example.com/image.png', params)
if data.success
puts 'Success! Optimized image URL: ' + data.kraked_url
else
puts 'Fail. Error message: ' + data.message
end
package main
import (
"log"
"io/ioutil"
"encoding/json"
"github.com/kraken-io/kraken-go"
)
func main() {
kr, err := kraken.New("your_api_key", "your_api_secret")
if err != nil {
log.Fatal(err)
}
credentialsFile, err := ioutil.ReadFile("path/to/your/credentials.json")
if err != nil {
log.Fatal(err)
}
var credentials map[string]interface{}
json.Unmarshal(credentialsFile, &credentials)
params := map[string]interface{} {
"url": "https://example.com/image.png",
"wait": true,
"lossy": true,
"gcs_store": map[string]interface {} {
"acl": "private",
"bucket": "your-bucket-name",
"path": "path/to/your/image.png",
"credentials": credentials,
}
}
data, err := kr.URL(params)
if err != nil {
log.Fatal(err)
}
if data["success"] != true {
log.Println("Failed, error message ", data["message"])
} else {
log.Println("Success, Optimized image URL: ", data["kraked_url"])
}
}
using System;
using System.IO;
using Newtonsoft.Json;
using Kraken.Http;
class Program
{
static void Main(string[] args)
{
string apiKey = "your_api_key";
string apiSecret = "your_api_secret";
var client = new KrakenHttpClient(apiKey, apiSecret);
string credentialsJson = File.ReadAllText("path/to/your/credentials.json");
var credentials = JsonConvert.DeserializeObject(credentialsJson);
var gcsStoreOptions = new
{
acl = "private",
bucket = "your-bucket-name",
path = "path/to/your/image.png",
credentials = credentials
};
var optimizeWaitRequest = new
{
url = "https://example.com/image.png",
wait = true,
lossy = true,
gcs_store = gcsStoreOptions
};
var response = client.Post("https://api.kraken.io/v1/url", optimizeWaitRequest);
if (response.success)
{
var responseBody = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Success! Optimized image URL: " + responseBody);
}
else
{
Console.WriteLine("Fail. Error message: " + response.ReasonPhrase);
}
}
}
Replace placeholders with your actual GCS bucket details and service account credentials (JSON file content).