netstorage

package module
v0.0.0-...-8ad0c09 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

README

Build Status Go Report Card GoDoc License

NetstorageKit-Golang: Akamai Netstorage API for Go

NetstorageKit-Golang is Akamai Netstorage (File/Object Store) API for Go 1.4+.

Installation

To install Netstorage API for Go:

$ go get github.com/darwayne/netstoragekit-golang

Example

package main

import (
  "fmt"
  "github.com/darwayne/netstoragekit-golang"
  "./secrets" // in the .gitignore file
)

func main() {
  nsHostname := "astin-nsu.akamaihd.net"
  nsKeyname  := "astinapi"
  nsKey := secrets.KEY // Don't expose nsKey on public repository.
  nsCpcode := "360949"

  ns := netstorage.NewNetstorage(nsHostname, nsKeyname, nsKey, false)

  localSource := "hello.txt"
  nsDestination := fmt.Sprintf("/%s/hello.txt", nsCpcode) // or "/%s/" is same. 

  res, body, err := ns.Upload(localSource, nsDestination)
  if err != nil {
      // Do something
  }

  if res.StatusCode == 200 {
      fmt.Printf(body)
  }
}

Methods

ns.Delete(netstoragePath)
ns.Dir(netstoragePath)
ns.Download(netstorageSource, localDestintation)
ns.Du(netstoragePath)
ns.Mkdir(netstoragePath + newDirectory)
ns.Mtime(netstoragePath, mTime) // ex) mTime: time.Now().Unix()
ns.QuickDelete(netstorageDir) // needs to the privilege on the CP Code
ns.Rename(netstorageTarget, netstorageDestination)
ns.Rmdir(netstorageDir) // remove empty direcoty
ns.Stat(netstoragePath)
ns.Symlink(netstorageTarget, netstorageDestination)
ns.Upload(localSource, netstorageDestination)

// INFO: can "Upload" Only a single file, not directory.

Test

You can test all above methods with the unittest script (NOTE: You should input nsHostname, nsKeyname, nsKey and nsCpcode in the script):

$ go test
### Netstorage Test ###
[TEST] Dir /360949 done
[TEST] Mkdir /360949/nst_1477474457 done
[TEST] Upload nst_1477474457.txt to /360949/nst_1477474457/nst_1477474457.txt done
[TEST] Du /360949/nst_1477474457 done
[TEST] Mtime /360949/nst_1477474457/nst_1477474457.txt done
[TEST] Stat /360949/nst_1477474457/nst_1477474457.txt done
[TEST] Symlink /360949/nst_1477474457/nst_1477474457.txt to /360949/nst_1477474457/nst_1477474457.txt_lnk done
[TEST] Rename /360949/nst_1477474457/nst_1477474457.txt to /360949/nst_1477474457/nst_1477474457.txt_rename done
[TEST] Download /360949/nst_1477474457/nst_1477474457.txt done
[TEST] delete /360949/nst_1477474457/nst_1477474457.txt_rename done
[TEST] delete /360949/nst_1477474457/nst_1477474457.txt_lnk done
[TEST] rmdir /360949/nst_1477474457 done

### Error Test ###
[TEST] Dir: netstorage invalid path test done
[TEST] Upload: local invalid path test done
[TEST] Download: netstorage directory path test done

PASS
[TEARDOWN] remove nst_1477474457.txt from local done
[TEARDOWN] remove nst_1477474457.txt_rename from local done
ok  	github.com/darwayne/netstoragekit-golang	x.xxxs

Author

Astin Choi (achoi@akamai.com)

License

Copyright 2016 Akamai Technologies, Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

Package netstorage provides interfacing the Akamai Netstorage(File/Object Store) API http(s) call

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Netstorage

type Netstorage struct {
	Hostname string
	Keyname  string
	Key      string
	Ssl      string
	Client   *http.Client
}

Netstorage struct provides all the necessary fields to create authorization headers. They are on the Akamai Netstorage account page. Hostname format should be "-nsu.akamaihd.net" and Note that don't expose Key on public repository. "Ssl" element is decided by "NetNetstorage" function - string "s" means https and "" does http.

func NewNetstorage

func NewNetstorage(hostname, keyname, key string, ssl bool) *Netstorage

NewNetstorage func creates and initiates Netstorage struct. ssl parameter decides https(true) and http(false) which means "s" and "".

func (*Netstorage) Delete

func (ns *Netstorage) Delete(nsPath string) (*http.Response, string, error)

Delete deletes an object/symbolic link

func (*Netstorage) DeleteWithContext

func (ns *Netstorage) DeleteWithContext(ctx context.Context, nsPath string) (*http.Response, string, error)

DeleteWithContext provides Delete behavior with context

func (*Netstorage) Dir

func (ns *Netstorage) Dir(nsPath string) (*http.Response, string, error)

Dir returns the directory structure

func (*Netstorage) DirWithContext

func (ns *Netstorage) DirWithContext(ctx context.Context, nsPath string) (*http.Response, string, error)

DirWithContext provides Dir behavior with context

func (*Netstorage) Download

func (ns *Netstorage) Download(path ...string) (*http.Response, string, error)

Download returns the string "Download done" when the download completes. The first parameter is Netstorage source path and the second is Local destination path. If you put only the first parameter, it downloads to current local path with the first parameter's file name. From the third parameters will be ignored. Note that you can download only a file, not a directory.

func (*Netstorage) DownloadWithContext

func (ns *Netstorage) DownloadWithContext(ctx context.Context, path ...string) (*http.Response, string, error)

DownloadWithContext provides Download behavior with context

func (*Netstorage) Du

func (ns *Netstorage) Du(nsPath string) (*http.Response, string, error)

Du returns the disk usage information for a directory

func (*Netstorage) DuWithContext

func (ns *Netstorage) DuWithContext(ctx context.Context, nsPath string) (*http.Response, string, error)

DuWithContext adds Du behavior with context

func (*Netstorage) Mkdir

func (ns *Netstorage) Mkdir(nsPath string) (*http.Response, string, error)

Mkdir creates an empty directory

func (*Netstorage) MkdirWithContext

func (ns *Netstorage) MkdirWithContext(ctx context.Context, nsPath string) (*http.Response, string, error)

MkdirWithContext provides Mkdir behavior with context

func (*Netstorage) Mtime

func (ns *Netstorage) Mtime(nsPath string, mtime int64) (*http.Response, string, error)

Mtime changes a file’s mtime

func (*Netstorage) MtimeWithContext

func (ns *Netstorage) MtimeWithContext(ctx context.Context, nsPath string, mtime int64) (*http.Response, string, error)

MtimeWithContext provides Mtime behavior with context

func (*Netstorage) QuickDelete

func (ns *Netstorage) QuickDelete(nsPath string) (*http.Response, string, error)

QuickDelete deletes a directory (i.e., recursively delete a directory tree) In order to use this func, you need to the privilege on the CP Code.

func (*Netstorage) QuickDeleteWithContext

func (ns *Netstorage) QuickDeleteWithContext(ctx context.Context, nsPath string) (*http.Response, string, error)

QuickDeleteWithContext provides QuickDelete behavior with context.

func (*Netstorage) Rename

func (ns *Netstorage) Rename(nsTarget, nsDestination string) (*http.Response, string, error)

Rename renames a file or symbolic link.

func (*Netstorage) RenameWithContext

func (ns *Netstorage) RenameWithContext(ctx context.Context, nsTarget, nsDestination string) (*http.Response, string, error)

RenameWithContext provides Rename behavior with context.

func (*Netstorage) Rmdir

func (ns *Netstorage) Rmdir(nsPath string) (*http.Response, string, error)

Rmdir deletes an empty directory

func (*Netstorage) RmdirWithContext

func (ns *Netstorage) RmdirWithContext(ctx context.Context, nsPath string) (*http.Response, string, error)

RmdirWithContext provides Rmdir behavior with context

func (*Netstorage) Stat

func (ns *Netstorage) Stat(nsPath string) (*http.Response, string, error)

Stat returns the information about an object structure

func (*Netstorage) StatWithContext

func (ns *Netstorage) StatWithContext(ctx context.Context, nsPath string) (*http.Response, string, error)

StatWithContext provides Stat behavior with context

func (ns *Netstorage) Symlink(nsTarget, nsDestination string) (*http.Response, string, error)

Symlink creates a symbolic link.

func (*Netstorage) SymlinkWithContext

func (ns *Netstorage) SymlinkWithContext(ctx context.Context, nsTarget, nsDestination string) (*http.Response, string, error)

SymlinkWithContext adds SymLink behavior with context.

func (*Netstorage) Upload

func (ns *Netstorage) Upload(localSource, nsDestination string) (*http.Response, string, error)

Upload uploads an object. The first parameter is the local source path and the second is the Netstorage destination path. If you put the directory path on "nsDestination" parameter, that filename will be the "localSource" parameter filename. Note that you can upload only a file, not a directory.

func (*Netstorage) UploadContent

func (ns *Netstorage) UploadContent(reader io.Reader, nsDestination string) (*http.Response, string, error)

UploadContent uploads an object directly with its content.

func (*Netstorage) UploadContentWithContext

func (ns *Netstorage) UploadContentWithContext(ctx context.Context, reader io.Reader, nsDestination string) (*http.Response, string, error)

UploadContentWithContext adds UploadContent behavior with context.

func (*Netstorage) UploadWithContext

func (ns *Netstorage) UploadWithContext(ctx context.Context, localSource, nsDestination string) (*http.Response, string, error)

UploadWithContext adds Upload behavior with context.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL