Categorygithub.com/gildas/go-box
modulepackage
0.2.1
Repository: https://github.com/gildas/go-box.git
Documentation: pkg.go.dev

# README

go-box

GoVersion GoDoc License Report

go-box is a Go client library for accessing the Box.com API.

Installation

go get github.com/gildas/go-box

Usage

After importing the package in your code:

import "github.com/gildas/go-box"

You can create a new client with:

client := box.NewClient(context)

Authentication

To let the client authenticate with nox.com, you need to provide credentials and authenticate:

client := box.NewClient(context)
creds := box.Credentials{
	ClientID:     "your-client-id",
	ClientSecret: "your-client-secret",
	EnterpriseID: "your-enterprise-id",
	AppAuth: box.AppAuth{
		PublicKeyID: "your-public-key-id",
		PrivateKey:  "your-private-key",
		PassPhrase: "your-passphrase",
	},
}
if err := client.Authenticate(creds); err != nil {
	log.Errorf("Failed to authenticate.", err)
}

Uploading a file

To upload a file you need to know the owner Id of the folder you want to upload the file to:

import "github.com/gildas/go-box"
import "github.com/gildas/go-logger"
import "github.com/gildas/go-request"

func UploadFile(context context.Context, client *box.Client, ownerID string, filename string, reader io.Reader) (*box.FileCollection, error) {
	log := logger.Must(logger.FromContext(context)).Child("box", "upload", "owner", ownerID)

	// Get the folder for the owner
	log.Debugf("Getting the folder for the owner.")
	folder, err := client.Folders.FindByName(context, ownerID)
	if err != nil {
		log.Errorf("Failed to get the folder for the owner.", err)
		return nil, err
	}

	return client.Files.Upload(context, &box.UploadOptions{
		Filename: filename,
		Parent:   folder.AsPathEntry(),
		Content:  request.ContentFromReader(reader),
	})
}

You can also upload payloads directly:

files, err := client.Files.Upload(context, &box.UploadOptions{
	Parent:   folder.AsPathEntry(),
	Filename: "test.json",
	Payload: struct {
		Name string `json:"name"`
	}{
		Name: "Test",
	},
})

Finding files

To find files, you can use the Find methods:

root, err := client.Folders.FindByID(context, "0")
entry, err := client.Files.FindByName(context, "test.json", root.AsPathEntry())

You can also find files by Id (coming from a previous upload, for example):

root, err := client.Folders.FindByID(context, "0")
entry, err := client.Files.FindByID(context, "1234567890", root.AsPathEntry())

Downloading a file

To download a file, you need the entry (see above):

downloaded, err := client.Files.Download(context, root.ItemCollection.Paths[0])

downloaded is a request.Content that you can use to read the content of the file.

Deleting a file

To delete a file:

err = client.Folders.Delete(context, root.ItemCollection.Paths[0])

Creating a folder

To create a folder, you need their parent folder:

root, err := client.Folders.FindByID(context, "0")
folder, err := client.Folders.Create(context, &box.FolderEntry{Name: "New Folder", Parent: root.AsPathEntry()})

Finding a folder

To find a folder, you can use the Find methods:

root, err := client.Folders.FindByID(context, "0")
entry, err := client.Folders.FindByName(context, "New Folder", root.AsPathEntry())

You can also find folders by Id (coming from a previous creation, for example):

root, err := client.Folders.FindByID(context, "0")
entry, err := client.Folders.FindByID(context, "1234567890", root.AsPathEntry())

Deleting a folder

To delete a folder:

err = client.Folders.Delete(context, root.ItemCollection.Paths[0])

Shared Links

To create a shared link:

link, err := client.SharedLinks.Create(context, &entry, nil)

You can also give some options to the shared link:

link, err := client.SharedLinks.Create(context, &entry, &box.SharedLinkOptions{
	Access: "open",
	Permissions: box.SharedLinkPermissions{
		CanDownload: true,
	},
	UnsharedAt: time.Now().Add(24 * time.Hour),
})

Logging

go-box uses go-logger for logging. The logs are bunyan compatible. You can either use the bunyan CLI to read them or use the lv tool to read them.

If you don't provider a logger, the client will create one for you that logs to os.Stderr with the INFO level.

To provide your own logger, you can do something like this (please have a look at go-logger for more information):

log := logger.Create("myapp", &logger.FileStream{Path: "myapp.log", FilterLevels: logger.NewLevelSet(logger.DEBUG)})
defer log.Flush()

client := box.NewClient(log.ToContext(context))

# Functions

NewClient instantiates a new Client.
TokenFromContext retrieves a token from the given context If no token was stored in the context, nil is returned.

# Constants

TokenContextKey is the key for the token stored in a context.Context.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
VERSION is the version of this application.

# Structs

AppAuth is used to authenticate an application.
Auth module.
Client is the Box Client.
ContextInfo gives some contextual information about the current error.
Credentials represents the Authentication information.
DownloadOptions contains the options for downloading data.
FileCollection represents a collection of FileEntry.
FileEntry represents a File Entry.
Files module.
FileVersion represents the version of a FileEntry.
FolderCollection represents a collection of FolderEntry.
FolderEntry represents a File Entry.
Folders module.
PathCollection represents a collection of PathEntry.
PathEntry represents a Path Entry.
Permissions exresses what is allowed on objects.
RequestError represents errors as returned by the BOX.com API.
SharedLink represents a shared link.
SharedLinkOptions contains the shared link options.
SharedLinks module.
Token is the token used to send requests to Box.com.
UploadOptions contains the options for uploading data.
UserEntry represents a User in a FileEntry.