commit
12dde94a39
@ -0,0 +1,51 @@
|
||||
package gcl
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Delete
|
||||
func (storage *Storage) Delete(path string) (bool, error) {
|
||||
// Create HTTP Request
|
||||
request, err := http.NewRequest("DELETE", fmt.Sprintf("https://%s/%s/%s", storage.endpoint, storage.zoneName, path), nil)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
// Add Headers
|
||||
request.Header.Add("AccessKey", storage.accessKey)
|
||||
// Do Request
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
// Close Response
|
||||
defer response.Body.Close()
|
||||
// Check Response
|
||||
if response.StatusCode/100 != 2 {
|
||||
// Read Body
|
||||
body, err := io.ReadAll(response.Body)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
// Response Data
|
||||
responseData := struct {
|
||||
HttpCode int `json:"HttpCode"`
|
||||
Message string `json:"Message"`
|
||||
}{}
|
||||
// Unmarshal Body
|
||||
err = json.Unmarshal(body, &responseData)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return false, fmt.Errorf("%d : %s", responseData.HttpCode, responseData.Message)
|
||||
}
|
||||
// Success
|
||||
return true, err
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package gcl
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Download
|
||||
func (storage *Storage) Download(path string) ([]byte, error) {
|
||||
// Create HTTP Request
|
||||
request, err := http.NewRequest("GET", fmt.Sprintf("https://%s/%s/%s", storage.endpoint, storage.zoneName, path), nil)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Add Headers
|
||||
request.Header.Add("accept", "*/*")
|
||||
request.Header.Add("AccessKey", storage.accessKey)
|
||||
// Do Request
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Close Response
|
||||
defer response.Body.Close()
|
||||
// Read Body
|
||||
body, err := io.ReadAll(response.Body)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Check Response
|
||||
if response.StatusCode/100 != 2 {
|
||||
// Response Data
|
||||
responseData := struct {
|
||||
HttpCode int `json:"HttpCode"`
|
||||
Message string `json:"Message"`
|
||||
}{}
|
||||
// Unmarshal Body
|
||||
err := json.Unmarshal(body, &responseData)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, fmt.Errorf("%d : %s", response.StatusCode, responseData.Message)
|
||||
}
|
||||
// Success
|
||||
return body, err
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package gcl
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// List
|
||||
func (storage *Storage) List(path string) ([]Object, error) {
|
||||
// Create HTTP Request
|
||||
request, err := http.NewRequest("GET", fmt.Sprintf("https://%s/%s/%s", storage.endpoint, storage.zoneName, path), nil)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Add Headers
|
||||
request.Header.Add("accept", "application/json")
|
||||
request.Header.Add("AccessKey", storage.accessKey)
|
||||
// Do Request
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Close Response
|
||||
defer response.Body.Close()
|
||||
// Read Body
|
||||
body, err := io.ReadAll(response.Body)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Check Response
|
||||
if response.StatusCode/100 != 2 {
|
||||
// Response Data
|
||||
responseData := struct {
|
||||
HttpCode int `json:"HttpCode"`
|
||||
Message string `json:"Message"`
|
||||
}{}
|
||||
// Unmarshal Body
|
||||
err = json.Unmarshal(body, &responseData)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, fmt.Errorf("%d : %s", responseData.HttpCode, responseData.Message)
|
||||
}
|
||||
// Response Data
|
||||
var responseData []Object
|
||||
// Unmarshal Body
|
||||
err = json.Unmarshal(body, &responseData)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Success
|
||||
return responseData, err
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package gcl
|
||||
|
||||
// New
|
||||
func New(accessKey string, endpoint string, zoneName string) Storage {
|
||||
return Storage{
|
||||
accessKey: accessKey,
|
||||
endpoint: endpoint,
|
||||
zoneName: zoneName,
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package gcl
|
||||
|
||||
// Object
|
||||
type Object struct {
|
||||
Guid string `json:"Guid"`
|
||||
StorageZoneName string `json:"StorageZoneName"`
|
||||
Path string `json:"Path"`
|
||||
ObjectName string `json:"ObjectName"`
|
||||
Length int `json:"Length"`
|
||||
LastChanged string `json:"LastChanged"`
|
||||
ServerId int `json:"ServerId"`
|
||||
ArrayNumber int `json:"ArrayNumber"`
|
||||
IsDirectory bool `json:"IsDirectory"`
|
||||
UserId string `json:"UserId"`
|
||||
ContentType string `json:"ContentType"`
|
||||
DateCreated string `json:"DateCreated"`
|
||||
StorageZoneId int `json:"StorageZoneId"`
|
||||
Checksum string `json:"Checksum"`
|
||||
ReplicatedZones string `json:"ReplicatedZones"`
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package gcl
|
||||
|
||||
// Storage
|
||||
type Storage struct {
|
||||
accessKey string
|
||||
endpoint string
|
||||
zoneName string
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package gcl
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Upload
|
||||
func (storage *Storage) Upload(path string, data []byte) (bool, error) {
|
||||
// Create HTTP Request
|
||||
request, err := http.NewRequest("PUT", fmt.Sprintf("https://%s/%s/%s", storage.endpoint, storage.zoneName, path), bytes.NewReader(data))
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
// Hasher
|
||||
hasher := sha256.New()
|
||||
hasher.Write(data)
|
||||
// Add Headers
|
||||
request.Header.Set("AccessKey", storage.accessKey)
|
||||
request.Header.Set("Content-Type", "application/octet-stream")
|
||||
request.Header.Set("Checksum", strings.ToUpper(hex.EncodeToString(hasher.Sum(nil))))
|
||||
// Do Request
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
// Close Response
|
||||
defer response.Body.Close()
|
||||
// Check Response
|
||||
if response.StatusCode/100 != 2 {
|
||||
// Read Body
|
||||
body, err := io.ReadAll(response.Body)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
// Response Data
|
||||
responseData := struct {
|
||||
HttpCode int `json:"HttpCode"`
|
||||
Message string `json:"Message"`
|
||||
}{}
|
||||
// Unmarshal Body
|
||||
err = json.Unmarshal(body, &responseData)
|
||||
// Handle Error
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return false, fmt.Errorf("%d : %s", responseData.HttpCode, responseData.Message)
|
||||
}
|
||||
// Success
|
||||
return true, err
|
||||
}
|
Loading…
Reference in New Issue