Explorar o código

Merge pull request #55 from aliyun/signurl

Signurl
baiyubin %!s(int64=8) %!d(string=hai) anos
pai
achega
dee3ad3086
Modificáronse 4 ficheiros con 164 adicións e 14 borrados
  1. 13 14
      README.md
  2. 3 0
      sample.go
  3. 74 0
      sample/archive.go
  4. 74 0
      sample/sign_url.go

+ 13 - 14
README.md

@@ -15,16 +15,16 @@
 ## Version
 > - Current version: 1.5.0. 
 
-## Run environment
+## Running Environment
 > - Go 1.4 or above is recommended. 
 
-## Install OSS Go SDK
+## Installing
 ### Install the SDK through GitHub
 > - Run the 'go get github.com/aliyun/aliyun-oss-go-sdk/oss' command to get the remote code package.
 > - Use 'import "github.com/aliyun/aliyun-oss-go-sdk/oss"' in your code to introduce OSS Go SDK package.
 
-## Quick use
-#### Get the bucket list (List Bucket)
+## Getting Started
+### List Bucket
 ```go
     client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
     if err != nil {
@@ -41,7 +41,7 @@
     }
 ```
 
-#### Create a bucket (Create Bucket)
+### Create Bucket
 ```go
     client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
     if err != nil {
@@ -54,7 +54,7 @@
     }
 ```
     
-#### Delete a bucket (Delete Bucket)
+### Delete Bucket
 ```go
     client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
     if err != nil {
@@ -67,7 +67,7 @@
     }
 ```
 
-#### Upload a file (Put Object)
+### Put Object
 ```go
     client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
     if err != nil {
@@ -85,7 +85,7 @@
     }
 ```
 
-#### Download an object (Get Object)
+### Get Object
 ```go
     client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
     if err != nil {
@@ -103,7 +103,7 @@
     }
 ```
 
-#### Get the object list (List Objects)
+### List Objects
 ```go
     client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
     if err != nil {
@@ -125,7 +125,7 @@
     }
 ```
     
-#### Delete an object (Delete Object)
+### Delete Object
 ```go
     client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
     if err != nil {
@@ -143,18 +143,17 @@
     }
 ```
 
-#### Others
+##  Complete Example
 More example projects can be found at 'src\github.com\aliyun\aliyun-oss-go-sdk\sample' under the installation path of the OSS Go SDK (the first path of the GOPATH variable). The directory contains example projects. 
 Or you can refer to the example objects in the sample directory under 'https://github.com/aliyun/aliyun-oss-go-sdk'.
 
-## Notes
-### Run a sample project
+### Running Example
 > - Copy the example file. Go to the installation path of OSS Go SDK (the first path of the GOPATH variable), enter the code directory of the OSS Go SDK, namely 'src\github.com\aliyun\aliyun-oss-go-sdk',
 and copy the sample directory and sample.go to the src directory of your test project.
 > - Modify the  endpoint, AccessKeyId, AccessKeySecret and BucketName configuration settings in sample/config.go.
 > - Run 'go run src/sample.go' under your project directory.
 
-## Contact us
+## Contacting us
 > - [Alibaba Cloud OSS official website](http://oss.aliyun.com).
 > - [Alibaba Cloud OSS official forum](http://bbs.aliyun.com).
 > - [Alibaba Cloud OSS official documentation center](http://www.aliyun.com/product/oss#Docs).

+ 3 - 0
sample.go

@@ -27,6 +27,9 @@ func main() {
 	sample.GetObjectSample()
 
 	sample.CnameSample()
+	sample.SignURLSample()
+
+	sample.ArchiveSample()
 
 	fmt.Println("All samples completed")
 }

+ 74 - 0
sample/archive.go

@@ -0,0 +1,74 @@
+package sample
+
+import (
+	"fmt"
+	"strings"
+	"time"
+
+	"github.com/aliyun/aliyun-oss-go-sdk/oss"
+)
+
+// ArchiveSample Archive Sample
+func ArchiveSample() {
+	// create archive bucket
+	client, err := oss.New(endpoint, accessID, accessKey)
+	if err != nil {
+		HandleError(err)
+	}
+
+	err = client.CreateBucket(bucketName, oss.StorageClass(oss.StorageArchive))
+	if err != nil {
+		HandleError(err)
+	}
+
+	archiveBucket, err := client.Bucket(bucketName)
+	if err != nil {
+		HandleError(err)
+	}
+
+	// put archive object
+	var val = "花间一壶酒,独酌无相亲。 举杯邀明月,对影成三人。"
+	err = archiveBucket.PutObject(objectKey, strings.NewReader(val))
+	if err != nil {
+		HandleError(err)
+	}
+
+	// check whether the object is archive class
+	meta, err := archiveBucket.GetObjectDetailedMeta(objectKey)
+	if err != nil {
+		HandleError(err)
+	}
+
+	if meta.Get("X-Oss-Storage-Class") == string(oss.StorageArchive) {
+		// restore object
+		err = archiveBucket.RestoreObject(objectKey)
+		if err != nil {
+			HandleError(err)
+		}
+
+		// wait for restore completed
+		meta, err = archiveBucket.GetObjectDetailedMeta(objectKey)
+		for meta.Get("X-Oss-Restore") == "ongoing-request=\"true\"" {
+			fmt.Println("x-oss-restore:" + meta.Get("X-Oss-Restore"))
+			time.Sleep(1000 * time.Second)
+			meta, err = archiveBucket.GetObjectDetailedMeta(objectKey)
+		}
+	}
+
+	// get restored object
+	err = archiveBucket.GetObjectToFile(objectKey, localFile)
+	if err != nil {
+		HandleError(err)
+	}
+
+	// restore repeatedly
+	err = archiveBucket.RestoreObject(objectKey)
+
+	// delete object and bucket
+	err = DeleteTestBucketAndObject(bucketName)
+	if err != nil {
+		HandleError(err)
+	}
+
+	fmt.Println("ArchiveSample completed")
+}

+ 74 - 0
sample/sign_url.go

@@ -0,0 +1,74 @@
+package sample
+
+import (
+	"fmt"
+	"io/ioutil"
+	"strings"
+
+	"github.com/aliyun/aliyun-oss-go-sdk/oss"
+)
+
+// SignURLSample sign url sample
+func SignURLSample() {
+	// 创建Bucket
+	bucket, err := GetTestBucket(bucketName)
+	if err != nil {
+		HandleError(err)
+	}
+
+	// put object
+	signedURL, err := bucket.SignURL(objectKey, oss.HTTPPut, 60)
+	if err != nil {
+		HandleError(err)
+	}
+
+	var val = "花间一壶酒,独酌无相亲。 举杯邀明月,对影成三人。"
+	err = bucket.PutObjectWithURL(signedURL, strings.NewReader(val))
+	if err != nil {
+		HandleError(err)
+	}
+
+	// put object with option
+	options := []oss.Option{
+		oss.Meta("myprop", "mypropval"),
+		oss.ContentType("image/tiff"),
+	}
+
+	signedURL, err = bucket.SignURL(objectKey, oss.HTTPPut, 60, options...)
+	if err != nil {
+		HandleError(err)
+	}
+
+	err = bucket.PutObjectFromFileWithURL(signedURL, localFile, options...)
+	if err != nil {
+		HandleError(err)
+	}
+
+	// get object
+	signedURL, err = bucket.SignURL(objectKey, oss.HTTPGet, 60)
+	if err != nil {
+		HandleError(err)
+	}
+
+	body, err := bucket.GetObjectWithURL(signedURL)
+	if err != nil {
+		HandleError(err)
+	}
+	// read content
+	data, err := ioutil.ReadAll(body)
+	body.Close()
+	data = data // use data
+
+	err = bucket.GetObjectToFileWithURL(signedURL, "mynewfile-1.jpg")
+	if err != nil {
+		HandleError(err)
+	}
+
+	// 删除object和bucket
+	err = DeleteTestBucketAndObject(bucketName)
+	if err != nil {
+		HandleError(err)
+	}
+
+	fmt.Println("SignURLSample completed")
+}