ossslim

package module
Version: v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2021 License: MIT Imports: 15 Imported by: 1

README

ossslim

Slim Aliyun OSS client.

You can create (upload), get (download), delete, list files on OSS.

Usage:

package main

import (
	"bytes"
	"crypto/rand"
	"fmt"
	"time"

	"github.com/caiguanhao/ossslim"
)

func main() {
	client := ossslim.Client{
		AccessKeyId:     "xxxxxxxxxxxxxxxx",
		AccessKeySecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
		Prefix:          "https://abc.oss-cn-hongkong.aliyuncs.com",
		Bucket:          "abc",
	}

	buf := make([]byte, 1024)
	_, err := rand.Read(buf)
	if err != nil {
		panic(err)
	}

	file := time.Now().UTC().Format("tmp/20060102150405")
	req, err := client.Upload(file, bytes.NewReader(buf), nil, "")
	if err != nil {
		panic(err)
	}
	fmt.Println(req.URL())
	// https://abc.oss-cn-hongkong.aliyuncs.com/tmp/20201229130519

	result, err := client.List("tmp/", false)
	if err != nil {
		panic(err)
	}
	fmt.Println(result.Files)
	// [{tmp/20201229130519 2020-12-29T13:05:20.000Z "13BB571C15022FC9211A85AF85272B85" 1024}]

	var buffer bytes.Buffer
	req, err = client.Download(file, &buffer)
	if err != nil {
		panic(err)
	}
	fmt.Println(bytes.Equal(buf, buffer.Bytes()))
	// true

	err = client.Delete(file)
	if err != nil {
		panic(err)
	}

	result, err = client.List("tmp/", false)
	if err != nil {
		panic(err)
	}
	fmt.Println(result.Files)
	// []
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	AccessKeyId     string
	AccessKeySecret string
	Prefix          string
	Bucket          string
}

An OSS client must have prefix, bucket, access key ID and access key secret. Prefix should be a string like this: https://<your-bucket>.<region>.aliyuncs.com.

func (*Client) Delete

func (c *Client) Delete(remotes ...string) error

Delete wraps DeleteWithContext using context.Background.

func (*Client) DeleteWithContext added in v1.1.0

func (c *Client) DeleteWithContext(ctx context.Context, remotes ...string) error

Delete creates and executes a delete request for multiple remote keys (paths) at the same time.

func (*Client) Download

func (c *Client) Download(remote string, respBody io.Writer) (*Request, error)

Download wraps DownloadWithContext using context.Background.

func (*Client) DownloadAsync

func (c *Client) DownloadAsync(remote string, respBody io.Writer) (*Request, error)

DownloadAsync wraps DownloadAsyncWithContext using context.Background.

func (*Client) DownloadAsyncWithContext added in v1.1.0

func (c *Client) DownloadAsyncWithContext(ctx context.Context, remote string, respBody io.Writer) (*Request, error)

DownloadAsync is like Download but won't wait till download is complete.

func (*Client) DownloadWithContext added in v1.1.0

func (c *Client) DownloadWithContext(ctx context.Context, remote string, respBody io.Writer) (*Request, error)

Download creates and executes a download request from remote path to respBody (io.Writer), returns the request and error. You can use bytes.Buffer to download the file to memory. If you want to have more than one destination, use io.MultiWriter.

func (*Client) Exists added in v1.1.0

func (c *Client) Exists(remote string) (bool, *Request, error)

func (*Client) ExistsWithContext added in v1.1.0

func (c *Client) ExistsWithContext(ctx context.Context, remote string) (exists bool, req *Request, err error)

func (*Client) List

func (c *Client) List(prefix string, recursive bool) (ListResult, error)

List wraps ListWithContext using context.Background.

func (*Client) ListWithContext added in v1.1.0

func (c *Client) ListWithContext(ctx context.Context, prefix string, recursive bool) (result ListResult, err error)

List creates and executes a list request for remote files and directories under prefix, recursively if recursive is set to true.

func (*Client) URL

func (c *Client) URL(remote string) string

URL generates URL without query string for remote file.

func (*Client) Upload

func (c *Client) Upload(remote string, reqBody io.Reader, reqBodyMd5 []byte, contentType string) (*Request, error)

Upload wraps UploadWithContext using context.Background.

func (*Client) UploadWithContext added in v1.1.0

func (c *Client) UploadWithContext(ctx context.Context, remote string, reqBody io.Reader, reqBodyMd5 []byte, contentType string) (*Request, error)

Upload creates and executes a upload request for reqBody (io.Reader) to remote path, returns the request and error. reqBodyMd5 can be nil, OSS will run MD5 check if it is provided. If contentType is empty, "application/octet-stream" will be used. If the body is bytes, use bytes.NewReader. If it is a string, use strings.NewReader.

type Directory

type Directory struct {
	Name string `xml:"Prefix"`
}

type File

type File struct {
	Name         string `xml:"Key"`
	LastModified string
	ETag         string
	Size         int64
}

type ListResult

type ListResult struct {
	Prefix string
	Files  []File
	Dirs   []Directory
}

type Request

type Request struct {
	Response              *http.Response
	ResponseContentLength *int64
	// contains filtered or unexported fields
}

func (*Request) String

func (req *Request) String() string

func (*Request) URL

func (req *Request) URL() string

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL