Categorygithub.com/pixelbin-io/pixelbin-go/v3
package
3.1.0
Repository: https://github.com/pixelbin-io/pixelbin-go.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author

# README

Pixelbin Backend SDK for Golang

Pixelbin Backend SDK for Golang helps you integrate the core Pixelbin features with your application.

Getting Started

Getting started with Pixelbin Backend SDK for Golang

Installation

go get -u "github.com/pixelbin-io/pixelbin-go/v3"

Usage

Quick Example

import (
	"fmt"
	"os"
	"github.com/pixelbin-io/pixelbin-go/v3/sdk/platform"
)

func main() {
    // create pixelbin config object
    config := platform.NewPixelbinConfig(
        "API_TOKEN",
        "https://api.pixelbin.io",
    )
    // set oauthclient
    config.SetOAuthClient()

    // create pixelbin client object
    pixelbin := platform.NewPixelbinClient(config)

    file, _ := os.Open("/home/rohit/deidara/1.jpeg")

    // Parameters for FileUpload function
    params := platform.FileUploadXQuery{
        File: file,
    }
    result, err := pixelbin.Assets.FileUpload(params)

    if err != nil {
        fmt.Println(err)
        return;
    }
    fmt.Println(result)
}

Uploader

Upload

Uploads a file to PixelBin with greater control over the upload process.

Arguments

ArgumentTypeRequiredDescription
fileio.ReaderyesThe file to be uploaded. It can be any type that implements the io.Reader interface, such as an open file or a buffer.
pUploaderUploadXQueryyesparameters for the upload, including file name, path, format, access level, and more.
optsuploaderOption...noVariadic option functions that allow customization of the upload process, such as setting chunk size, maximum retries, concurrency, and exponential factor.

UploaderUploadXQuery Struct

FieldTypeRequiredDescription
NamestringnoName of the file.
PathstringnoPath of the containing folder.
FormatstringnoFormat of the file.
AccessAccessEnumnoAccess level of the asset, can be either public-read or private.
Tags[]stringnoTags associated with the file.
Metadatamap[string]interface{}noMetadata associated with the file.
OverwriteboolnoOverwrite flag. If set to true, will overwrite any file that exists with the same path, name, and type. Defaults to false.
FilenameOverrideboolnoIf set to true, will add unique characters to the name if an asset with the given name already exists. If Overwrite is also set to true, preference will be given to Overwrite. If both are set to false, an error will occur.
Expiryfloat64noExpiry time in seconds for the underlying signed URL. Defaults to 3000 seconds.

Uploader Options

These options can be passed as variadic arguments to fine-tune the upload process:

  • WithChunkSize(size uint): Set the size of each chunk to upload. Default is 10 megabytes.
  • WithMaxRetries(retries uint): Set the maximum number of retries if an upload fails. Default is 2 retries.
  • WithConcurrency(concurrency uint): Set the number of concurrent chunk upload tasks. Default is 3 concurrent chunk uploads.
  • WithExponentialFactor(factor uint): Set the exponential factor for retry delay. Default is 2.

Returns

  • On Success: map[string]interface{} containing details about the uploaded file, such as url, name, format, tags, and metadata.

Sure, here’s the updated "On Success" table using Go datatypes:

On Success

PropertyTypeDescriptionExample
orgIdintOrganization ID.5320086
typestringThe type of asset. Always "file"."file"
namestringName of the file."testfile.jpeg"
pathstringPath of the containing folder."/path/to/image.jpeg"
fileIdstringUnique ID of the file."testfile.jpeg"
accessstringAccess level of the asset, either "public-read" or "private"."public-read"
tags[]stringTags associated with the file.[]string{"tag1", "tag2"}
metadatamap[string]interface{}Metadata associated with the file.map[string]interface{}{"source": "", "publicUploadId": ""}
formatstringFile format."jpeg"
assetTypestringType of asset, e.g., "image"."image"
sizeint64File size in bytes.37394
widthintWidth of the file (if applicable).720
heightintHeight of the file (if applicable).450
contextmap[string]interface{}Contains file metadata and other context information.map[string]interface{}{"steps": [], "meta": { ... }}
isOriginalboolIndicates if the file is the original.true
_idstringRecord ID of the uploaded file."a0b0b19a-d526-4xc07-ae51-0xxxxxx"
urlstringURL of the uploaded file."https://cdn.pixelbin.io/v2/user-e26cf3/original/testfile.jpeg"
  • On Error: An error describing what went wrong during the upload process.

Uploading a buffer

package main

import (
	"bytes"
	"fmt"
	"os"

	"github.com/pixelbin-io/pixelbin-go/v3/sdk/platform"
)

func main() {
    // Create PixelBin config object
    config := platform.NewPixelbinConfig("API_TOKEN", "https://api.pixelbin.io")
    config.SetOAuthClient()

    // Create PixelBin client object
    pixelbin := platform.NewPixelbinClient(config)

    // Open the file to be uploaded
    file, err := os.ReadFile("./path/to/your/file.png")
    if err != nil {
        fmt.Println("Error reading file:", err)
        return
    }

    buffer := bytes.NewReader(file)

    // Define the upload parameters
    params := platform.UploaderUploadXQuery{
        Name:    "myimage",
        Path:    "folder",
        Format:  "jpeg",
        Access:  "public-read",
		Overwrite:        true,
        FilenameOverride: false,
        Expiry:           3600, // 1 hour
    }

    // Upload the file with custom options
    result, err := pixelbin.Uploader.Upload(buffer, params,
        platform.WithChunkSize(5*1024*1024),  // 5MB
        platform.WithMaxRetries(3),
        platform.WithConcurrency(2),
        platform.WithExponentialFactor(2),
    )

    if err != nil {
        fmt.Println("Error uploading file:", err)
        return
    }

    // Print the result
    fmt.Println("File uploaded successfully:", result["url"])
}

Uploading a stream

package main

import (
	"fmt"
	"os"

	"github.com/pixelbin-io/pixelbin-go/v3/sdk/platform"
)

func main() {
    // Create PixelBin config object
    config := platform.NewPixelbinConfig("API_TOKEN", "https://api.pixelbin.io")
    config.SetOAuthClient()

    // Create PixelBin client object
    pixelbin := platform.NewPixelbinClient(config)

    // Open the file to be uploaded
    file, err := os.Open("./path/to/your/file.png")

    if err != nil {
        fmt.Println("Error opening file:", err)
        return;
    }

    // Define the upload parameters
    params := platform.UploaderUploadXQuery{
        Name:    "myimage",
        Path:    "folder",
        Format:  "jpeg",
        Access:  "public-read",
		Overwrite:        true,
        FilenameOverride: false,
        Expiry:           3600, // 1 hour
    }

    // Upload the file with custom options
    result, err := pixelbin.Uploader.Upload(file, params,
        platform.WithChunkSize(5*1024*1024),  // 5MB
        platform.WithMaxRetries(3),
        platform.WithConcurrency(2),
        platform.WithExponentialFactor(2),
    )

    if err != nil {
        fmt.Println("Error uploading file:", err)
        return
    }

    // Print the result
    fmt.Println("File uploaded successfully:", result["url"])
}

Security Utils

For generating Signed URLs

Generate a signed PixelBin url

ParameterDescriptionExample
url (string)A valid Pixelbin URL to be signedhttps://cdn.pixelbin.io/v2/dummy-cloudname/original/__playground/playground-default.jpeg
expirySeconds (int)Number of seconds the signed URL should be valid for20
accessKey (string)Access key of the token used for signinga45e52d8-21ac-4a97-bd4f-eb5dd58602e0
token (string)Value of the token used for signingdummy-token

Example:

package main

import (
	"fmt"
	"os"
	"github.com/pixelbin-io/pixelbin-go/v3/sdk/utils/security"
)

func main() {
    signedUrl, err := security.SignURL(
        "https://cdn.pixelbin.io/v2/dummy-cloudname/original/__playground/playground-default.jpeg", // urlString
        20, // expirySeconds
        "a45e52d8-21ac-4a97-bd4f-eb5dd58602e0", // accessKey
        "dummy-token", // token
    )
    if err != nil {
        fmt.Println(err)
        return;
    }
    fmt.Println(signedUrl)
}
// signed_url
// https://cdn.pixelbin.io/v2/dummy-cloudname/original/__playground/playground-default.jpeg?pbs=8eb6a00af74e57967a42316e4de238aa88d92961649764fad1832c1bff101f25&pbe=1695635915&pbt=a45e52d8-21ac-4a97-bd4f-eb5dd58602e0

Usage with custom domain url

package main

import (
	"fmt"
	"os"
	"github.com/pixelbin-io/pixelbin-go/v3/sdk/utils/security"
)

func main() {
    signedUrl, err := security.SignUrl(
        "https://krit.imagebin.io/v2/original/__playground/playground-default.jpeg", // url
        30, // expirySeconds
        "ab110791-db9f-4dca-ac39-d29db5941daa", // accessKey
        "dummy-token", // token
    )
    if err != nil {
        fmt.Println(err)
        return;
    }
    fmt.Println(signedUrl)
}
// signedUrl
// https://krit.imagebin.io/v2/original/__playground/playground-default.jpeg?pbs=1aef31c1e0ecd8a875b1d3184f324327f4ab4bce419d81d1eb1a818ee5f2e3eb&pbe=1695705975&pbt=ab110791-db9f-4dca-ac39-d29db5941daa

URL Utils

Pixelbin provides url utilities to construct and deconstruct Pixelbin urls.

UrlToObj

Deconstruct a pixelbin URL

ParameterDescriptionExample
pixelbinUrl (string)A valid pixelbin URLhttps://cdn.pixelbin.io/v2/your-cloud-name/z-slug/t.resize(h:100,w:200)~t.flip()/path/to/image.jpeg
opts (variadic)Functional options for configuring the function (optional)See UrlToObjOption below

UrlToObjOption:

UrlToObjOption is a functional option for configuring the UrlToObj function. You can use it to customize the behavior of the function by setting different options. See the table below for a list of available options.

Options:

OptionDescriptionDefault Value
WithCustomDomainSet IsCustomDomain to true or falsefalse

Returns:

PropertyDescriptionExample
baseURL (string)Base path of the URLhttps://cdn.pixelbin.io
filePath (string)Path to the file on Pixelbin storage/path/to/image.jpeg
version (string)Version of the URLv2
cloudName (string)Cloud name from the URLyour-cloud-name
transformations (array)A list of transformation objects[{ "plugin": "t", "name": "flip" }]
zone (string)Zone slug from the URLz-slug
pattern (string)Transformation pattern extracted from the URLt.resize(h:100,w:200)~t.flip()
worker (boolean)Indicates if the URL is a URL Translation Worker URLFalse
workerPath (string)Input path to a URL Translation Workerresize:w200,h400/folder/image.jpeg
options (Object)Query parameters added, such as "dpr" and "f_auto"{ dpr: 2.5, f_auto: True}

Example:

package main

import (
	"fmt"
	"os"
	"github.com/pixelbin-io/pixelbin-go/v3/sdk/utils/url"
)

func main() {
    pixelbinUrl := "https://cdn.pixelbin.io/v2/your-cloud-name/z-slug/t.resize(h:100,w:200)~t.flip()/path/to/image.jpeg?dpr=2.0&f_auto=true"
    obj, err := url.UrlToObj(pixelbinUrl)
    if err != nil {
        fmt.Println(err)
        return;
    }
    fmt.Println(obj)
}
// obj
// {
//     "cloudName": "your-cloud-name",
//     "zone": "z-slug",
//     "version": "v2",
//     "options": {
//         "dpr": "2.0",
//         "f_auto": "true",
//     },
//     "transformations": [
//         {
//             "plugin": "t",
//             "name": "resize",
//             "values": [
//                 {
//                     "key": "h",
//                     "value": "100"
//                 },
//                 {
//                     "key": "w",
//                     "value": "200"
//                 }
//             ]
//         },
//         {
//             "plugin": "t",
//             "name": "flip",
//         }
//     ],
//     "filePath": "path/to/image.jpeg",
//     "baseUrl": "https://cdn.pixelbin.io"
// }

Usage with custom domain

package main

import (
	"fmt"
	"os"
	"github.com/pixelbin-io/pixelbin-go/v3/sdk/utils/url"
)

func main() {
    customDomainUrl :=
        "https://xyz.designify.media/v2/z-slug/t.resize(h:100,w:200)~t.flip()/path/to/image.jpeg"
    obj, err := url.UrlToObj(customDomainUrl, url.WithCustomDomain(true))
    if err != nil {
        fmt.Println(err)
        return;
    }
    fmt.Println(obj)
}
// obj
// {
//     "zone": "z-slug",
//     "version": "v2",
//     "transformations": [
//         {
//             "plugin": "t",
//             "name": "resize",
//             "values": [
//                 {
//                     "key": "h",
//                     "value": "100"
//                 },
//                 {
//                     "key": "w",
//                     "value": "200"
//                 }
//             ]
//         },
//         {
//             "plugin": "t",
//             "name": "flip",
//         }
//     ],
//     "filePath": "path/to/image.jpeg",
//     "baseUrl": "https://xyz.designify.media",
//     "wrkr": False,
//     "workerPath": "",
//     "options": {}
// }

Usage with URL Translation Worker

package main

import (
	"fmt"
	"os"
	"github.com/pixelbin-io/pixelbin-go/v3/sdk/utils/url"
)

func main() {
    workerUrl :=
        "https://cdn.pixelbin.io/v2/your-cloud-name/z-slug/wrkr/resize:h100,w:200/folder/image.jpeg";
    obj, err := url.UrlToObj(workerUrl)
    if err != nil {
        fmt.Println(err)
        return;
    }
    fmt.Println(obj)
}
// obj
// {
//     "cloudName": "your-cloud-name",
//     "zone": "z-slug",
//     "version": "v2",
//     "transformations": [],
//     "filePath": "",
//     "worker": True,
//     "workerPath": "resize:h100,w:200/folder/image.jpeg",
//     "baseUrl": "https://cdn.pixelbin.io"
//     "options": {}
// }

ObjToUrl

Converts the extracted url obj to a Pixelbin url.

PropertyDescriptionExample
cloudName (string)The cloudname extracted from the URLyour-cloud-name
zone (string)6 character zone slugz-slug
version (string)CDN API versionv2
transformations (array)Extracted transformations from the URL[{ "plugin": "t", "name": "flip" }]
filePath (string)Path to the file on Pixelbin storage/path/to/image.jpeg
baseUrl (string)Base URLhttps://cdn.pixelbin.io/
isCustomDomain (boolean)Indicates if the URL is for a custom domainFalse
worker (boolean)Indicates if the URL is a URL Translation Worker URLFalse
workerPath (string)Input path to a URL Translation Workerresize:w200,h400/folder/image.jpeg
options (Object)Query parameters added, such as "dpr" and "f_auto"{ "dpr": 2.0, "f_auto": True }
package main

import (
	"fmt"
	"os"
	"github.com/pixelbin-io/pixelbin-go/v3/sdk/utils/url"
)
func main() {
    obj := map[string]interface{}{
        cloudName: "your-cloud-name",
        zone: "z-slug",
        version: "v2",
        options:  []map[string]interface{}{
            dpr: 2.5,
            f_auto: true,
        },
        transformations: []map[string]interface{}{
            {
                plugin: "t",
                name: "flop",
            },
            {
                plugin: "t",
                name: "flip",
            },
        },
        filePath: "path/to/image.jpeg",
        baseUrl: "https://cdn.pixelbin.io",
    }
    urlstring, err := url.ObjToUrl(obj) // obj is as shown above
    if err != nil {
        fmt.Println(err)
        return;
    }
    fmt.Println(urlstring)
}
// urlstring
// https://cdn.pixelbin.io/v2/your-cloud-name/z-slug/t.flop()~t.flip()/path/to/image.jpeg?dpr=2.5&f_auto=true

Usage with custom domain

package main

import (
	"fmt"
	"os"
	"github.com/pixelbin-io/pixelbin-go/v3/sdk/utils/url"
)
func main() {
    obj := map[string]interface{}{
        cloudName: "your-cloud-name",
        zone: "z-slug",
        version: "v2",
        options:  []map[string]interface{}{
            dpr: 2.5,
            f_auto: true,
        },
        transformations: []map[string]interface{}{
            {
                plugin: "t",
                name: "flop",
            },
            {
                plugin: "t",
                name: "flip",
            },
        },
        filePath: "path/to/image.jpeg",
        baseUrl: "https://xyz.designify.media",
        isCustomDomain: True,
    }
    urlstring, err := url.ObjToUrl(obj) // obj is as shown above
    if err != nil {
        fmt.Println(err)
        return;
    }
    fmt.Println(urlstring)
}
// urlstring
// https://xyz.designify.media/v2/z-slug/t.flop()~t.flip()/path/to/image.jpeg?dpr=2.5&f_auto=true

Usage with URL Translation Worker

package main

import (
	"fmt"
	"os"
	"github.com/pixelbin-io/pixelbin-go/v3/sdk/utils/url"
)
func main() {
    obj := map[string]interface{}{
        cloudName: "your-cloud-name",
        zone: "z-slug",
        version: "v2",
        options: []map[string]interface{}{
            dpr: 2.5,
            f_auto: true,
        },
        transformations: []map[string]interface{}{},
        worker: true,
        workerPath: "resize:h100,w:200/folder/image.jpeg",
        filePath: "path/to/image.jpeg",
        baseUrl: "https://cdn.pixelbin.io",
    }
    urlstring, err := url.ObjToUrl(obj) // obj is as shown above
    if err != nil {
        fmt.Println(err)
        return;
    }
    fmt.Println(urlstring)
}
// urlstring
// https://cdn.pixelbin.io/v2/your-cloud-name/z-slug/wrkr/resize:h100,w:200/folder/image.jpeg

Documentation