2015-06-12 00:30:18 +02:00
// Package storage provides clients for Microsoft Azure Storage Services.
2015-02-05 01:37:43 +01:00
package storage
import (
"bytes"
"encoding/base64"
2016-10-29 00:46:05 +02:00
"encoding/json"
2015-02-05 01:37:43 +01:00
"encoding/xml"
2016-02-08 23:29:21 +01:00
"errors"
2015-02-05 01:37:43 +01:00
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
2017-04-14 03:05:38 +02:00
"runtime"
2016-02-08 23:29:21 +01:00
"strconv"
2015-02-05 01:37:43 +01:00
"strings"
2017-04-14 03:05:38 +02:00
"github.com/Azure/go-autorest/autorest/azure"
2015-02-05 01:37:43 +01:00
)
const (
2017-04-14 03:05:38 +02:00
// DefaultBaseURL is the domain name used for storage requests in the
// public cloud when a default client is created.
2015-06-12 00:30:18 +02:00
DefaultBaseURL = "core.windows.net"
2017-04-14 03:05:38 +02:00
// DefaultAPIVersion is the Azure Storage API version string used when a
2015-06-12 00:30:18 +02:00
// basic client is created.
2017-04-14 03:05:38 +02:00
DefaultAPIVersion = "2015-04-05"
2015-06-12 00:30:18 +02:00
defaultUseHTTPS = true
2015-02-05 01:37:43 +01:00
2016-10-29 00:46:05 +02:00
// StorageEmulatorAccountName is the fixed storage account used by Azure Storage Emulator
StorageEmulatorAccountName = "devstoreaccount1"
// StorageEmulatorAccountKey is the the fixed storage account used by Azure Storage Emulator
StorageEmulatorAccountKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
2015-02-05 01:37:43 +01:00
blobServiceName = "blob"
tableServiceName = "table"
queueServiceName = "queue"
2016-02-08 23:29:21 +01:00
fileServiceName = "file"
2016-10-29 00:46:05 +02:00
storageEmulatorBlob = "127.0.0.1:10000"
storageEmulatorTable = "127.0.0.1:10002"
storageEmulatorQueue = "127.0.0.1:10001"
2017-04-14 03:05:38 +02:00
userAgentHeader = "User-Agent"
2015-02-05 01:37:43 +01:00
)
2015-06-12 00:30:18 +02:00
// Client is the object that needs to be constructed to perform
// operations on the storage account.
type Client struct {
2016-10-29 00:46:05 +02:00
// HTTPClient is the http.Client used to initiate API
// requests. If it is nil, http.DefaultClient is used.
HTTPClient * http . Client
2017-04-14 03:05:38 +02:00
accountName string
accountKey [ ] byte
useHTTPS bool
UseSharedKeyLite bool
baseURL string
apiVersion string
userAgent string
2015-02-05 01:37:43 +01:00
}
type storageResponse struct {
statusCode int
headers http . Header
body io . ReadCloser
}
2016-10-29 00:46:05 +02:00
type odataResponse struct {
storageResponse
odata odataErrorMessage
}
2015-06-12 00:30:18 +02:00
// AzureStorageServiceError contains fields of the error response from
2015-02-05 01:37:43 +01:00
// Azure Storage Service REST API. See https://msdn.microsoft.com/en-us/library/azure/dd179382.aspx
// Some fields might be specific to certain calls.
2015-06-12 00:30:18 +02:00
type AzureStorageServiceError struct {
2015-02-05 01:37:43 +01:00
Code string ` xml:"Code" `
Message string ` xml:"Message" `
AuthenticationErrorDetail string ` xml:"AuthenticationErrorDetail" `
QueryParameterName string ` xml:"QueryParameterName" `
QueryParameterValue string ` xml:"QueryParameterValue" `
Reason string ` xml:"Reason" `
StatusCode int
2015-06-12 00:30:18 +02:00
RequestID string
}
2016-10-29 00:46:05 +02:00
type odataErrorMessageMessage struct {
Lang string ` json:"lang" `
Value string ` json:"value" `
}
type odataErrorMessageInternal struct {
Code string ` json:"code" `
Message odataErrorMessageMessage ` json:"message" `
}
type odataErrorMessage struct {
Err odataErrorMessageInternal ` json:"odata.error" `
}
2015-06-12 00:30:18 +02:00
// UnexpectedStatusCodeError is returned when a storage service responds with neither an error
// nor with an HTTP status code indicating success.
type UnexpectedStatusCodeError struct {
allowed [ ] int
got int
}
func ( e UnexpectedStatusCodeError ) Error ( ) string {
s := func ( i int ) string { return fmt . Sprintf ( "%d %s" , i , http . StatusText ( i ) ) }
got := s ( e . got )
expected := [ ] string { }
for _ , v := range e . allowed {
expected = append ( expected , s ( v ) )
}
return fmt . Sprintf ( "storage: status code from service response is %s; was expecting %s" , got , strings . Join ( expected , " or " ) )
2015-02-05 01:37:43 +01:00
}
2016-02-08 23:29:21 +01:00
// Got is the actual status code returned by Azure.
func ( e UnexpectedStatusCodeError ) Got ( ) int {
return e . got
}
2015-06-12 00:30:18 +02:00
// NewBasicClient constructs a Client with given storage service name and
// key.
func NewBasicClient ( accountName , accountKey string ) ( Client , error ) {
2016-10-29 00:46:05 +02:00
if accountName == StorageEmulatorAccountName {
return NewEmulatorClient ( )
}
2015-06-12 00:30:18 +02:00
return NewClient ( accountName , accountKey , DefaultBaseURL , DefaultAPIVersion , defaultUseHTTPS )
2017-04-14 03:05:38 +02:00
}
2016-12-13 06:12:39 +01:00
2017-04-14 03:05:38 +02:00
// NewBasicClientOnSovereignCloud constructs a Client with given storage service name and
// key in the referenced cloud.
func NewBasicClientOnSovereignCloud ( accountName , accountKey string , env azure . Environment ) ( Client , error ) {
if accountName == StorageEmulatorAccountName {
return NewEmulatorClient ( )
}
return NewClient ( accountName , accountKey , env . StorageEndpointSuffix , DefaultAPIVersion , defaultUseHTTPS )
2015-02-05 01:37:43 +01:00
}
2016-10-29 00:46:05 +02:00
//NewEmulatorClient contructs a Client intended to only work with Azure
//Storage Emulator
func NewEmulatorClient ( ) ( Client , error ) {
return NewClient ( StorageEmulatorAccountName , StorageEmulatorAccountKey , DefaultBaseURL , DefaultAPIVersion , false )
}
2015-06-12 00:30:18 +02:00
// NewClient constructs a Client. This should be used if the caller wants
// to specify whether to use HTTPS, a specific REST API version or a custom
// storage endpoint than Azure Public Cloud.
func NewClient ( accountName , accountKey , blobServiceBaseURL , apiVersion string , useHTTPS bool ) ( Client , error ) {
var c Client
2015-02-05 01:37:43 +01:00
if accountName == "" {
2015-03-24 05:57:24 +01:00
return c , fmt . Errorf ( "azure: account name required" )
2015-02-05 01:37:43 +01:00
} else if accountKey == "" {
2015-03-24 05:57:24 +01:00
return c , fmt . Errorf ( "azure: account key required" )
2015-06-12 00:30:18 +02:00
} else if blobServiceBaseURL == "" {
2015-03-24 05:57:24 +01:00
return c , fmt . Errorf ( "azure: base storage service url required" )
2015-02-05 01:37:43 +01:00
}
key , err := base64 . StdEncoding . DecodeString ( accountKey )
if err != nil {
2016-10-29 00:46:05 +02:00
return c , fmt . Errorf ( "azure: malformed storage account key: %v" , err )
2015-02-05 01:37:43 +01:00
}
2017-04-14 03:05:38 +02:00
c = Client {
accountName : accountName ,
accountKey : key ,
useHTTPS : useHTTPS ,
baseURL : blobServiceBaseURL ,
apiVersion : apiVersion ,
UseSharedKeyLite : false ,
}
c . userAgent = c . getDefaultUserAgent ( )
return c , nil
}
func ( c Client ) getDefaultUserAgent ( ) string {
return fmt . Sprintf ( "Go/%s (%s-%s) Azure-SDK-For-Go/%s storage-dataplane/%s" ,
runtime . Version ( ) ,
runtime . GOARCH ,
runtime . GOOS ,
sdkVersion ,
c . apiVersion ,
)
}
// AddToUserAgent adds an extension to the current user agent
func ( c * Client ) AddToUserAgent ( extension string ) error {
if extension != "" {
c . userAgent = fmt . Sprintf ( "%s %s" , c . userAgent , extension )
return nil
}
return fmt . Errorf ( "Extension was empty, User Agent stayed as %s" , c . userAgent )
}
// protectUserAgent is used in funcs that include extraheaders as a parameter.
// It prevents the User-Agent header to be overwritten, instead if it happens to
// be present, it gets added to the current User-Agent. Use it before getStandardHeaders
func ( c * Client ) protectUserAgent ( extraheaders map [ string ] string ) map [ string ] string {
if v , ok := extraheaders [ userAgentHeader ] ; ok {
c . AddToUserAgent ( v )
delete ( extraheaders , userAgentHeader )
}
return extraheaders
2015-02-05 01:37:43 +01:00
}
2015-06-12 00:30:18 +02:00
func ( c Client ) getBaseURL ( service string ) string {
2015-02-05 01:37:43 +01:00
scheme := "http"
2015-06-12 00:30:18 +02:00
if c . useHTTPS {
2015-02-05 01:37:43 +01:00
scheme = "https"
}
2016-10-29 00:46:05 +02:00
host := ""
if c . accountName == StorageEmulatorAccountName {
switch service {
case blobServiceName :
host = storageEmulatorBlob
case tableServiceName :
host = storageEmulatorTable
case queueServiceName :
host = storageEmulatorQueue
}
} else {
host = fmt . Sprintf ( "%s.%s.%s" , c . accountName , service , c . baseURL )
}
2015-02-05 01:37:43 +01:00
u := & url . URL {
Scheme : scheme ,
Host : host }
return u . String ( )
}
2015-06-12 00:30:18 +02:00
func ( c Client ) getEndpoint ( service , path string , params url . Values ) string {
u , err := url . Parse ( c . getBaseURL ( service ) )
2015-02-05 01:37:43 +01:00
if err != nil {
// really should not be happening
panic ( err )
}
2016-10-29 00:46:05 +02:00
// API doesn't accept path segments not starting with '/'
if ! strings . HasPrefix ( path , "/" ) {
path = fmt . Sprintf ( "/%v" , path )
}
if c . accountName == StorageEmulatorAccountName {
path = fmt . Sprintf ( "/%v%v" , StorageEmulatorAccountName , path )
2015-02-05 01:37:43 +01:00
}
u . Path = path
u . RawQuery = params . Encode ( )
return u . String ( )
}
2015-06-12 00:30:18 +02:00
// GetBlobService returns a BlobStorageClient which can operate on the blob
// service of the storage account.
func ( c Client ) GetBlobService ( ) BlobStorageClient {
2017-04-14 03:05:38 +02:00
b := BlobStorageClient {
client : c ,
}
b . client . AddToUserAgent ( blobServiceName )
b . auth = sharedKey
if c . UseSharedKeyLite {
b . auth = sharedKeyLite
}
return b
2015-06-12 00:30:18 +02:00
}
// GetQueueService returns a QueueServiceClient which can operate on the queue
// service of the storage account.
func ( c Client ) GetQueueService ( ) QueueServiceClient {
2017-04-14 03:05:38 +02:00
q := QueueServiceClient {
client : c ,
}
q . client . AddToUserAgent ( queueServiceName )
q . auth = sharedKey
if c . UseSharedKeyLite {
q . auth = sharedKeyLite
}
return q
2015-02-05 01:37:43 +01:00
}
2016-10-29 00:46:05 +02:00
// GetTableService returns a TableServiceClient which can operate on the table
// service of the storage account.
func ( c Client ) GetTableService ( ) TableServiceClient {
2017-04-14 03:05:38 +02:00
t := TableServiceClient {
client : c ,
}
t . client . AddToUserAgent ( tableServiceName )
t . auth = sharedKeyForTable
if c . UseSharedKeyLite {
t . auth = sharedKeyLiteForTable
}
return t
2016-10-29 00:46:05 +02:00
}
2016-02-08 23:29:21 +01:00
// GetFileService returns a FileServiceClient which can operate on the file
// service of the storage account.
func ( c Client ) GetFileService ( ) FileServiceClient {
2017-04-14 03:05:38 +02:00
f := FileServiceClient {
client : c ,
2015-02-05 01:37:43 +01:00
}
2017-04-14 03:05:38 +02:00
f . client . AddToUserAgent ( fileServiceName )
f . auth = sharedKey
if c . UseSharedKeyLite {
f . auth = sharedKeyLite
}
return f
2015-02-05 01:37:43 +01:00
}
2015-06-12 00:30:18 +02:00
func ( c Client ) getStandardHeaders ( ) map [ string ] string {
2015-02-05 01:37:43 +01:00
return map [ string ] string {
2017-04-14 03:05:38 +02:00
userAgentHeader : c . userAgent ,
"x-ms-version" : c . apiVersion ,
"x-ms-date" : currentTimeRfc1123Formatted ( ) ,
2015-02-05 01:37:43 +01:00
}
}
2017-04-14 03:05:38 +02:00
func ( c Client ) exec ( verb , url string , headers map [ string ] string , body io . Reader , auth authentication ) ( * storageResponse , error ) {
headers , err := c . addAuthorizationHeader ( verb , url , headers , auth )
2015-02-05 01:37:43 +01:00
if err != nil {
return nil , err
}
req , err := http . NewRequest ( verb , url , body )
2016-02-08 23:29:21 +01:00
if err != nil {
return nil , errors . New ( "azure/storage: error creating request: " + err . Error ( ) )
}
if clstr , ok := headers [ "Content-Length" ] ; ok {
// content length header is being signed, but completely ignored by golang.
// instead we have to use the ContentLength property on the request struct
// (see https://golang.org/src/net/http/request.go?s=18140:18370#L536 and
// https://golang.org/src/net/http/transfer.go?s=1739:2467#L49)
req . ContentLength , err = strconv . ParseInt ( clstr , 10 , 64 )
if err != nil {
return nil , err
}
}
2015-02-05 01:37:43 +01:00
for k , v := range headers {
req . Header . Add ( k , v )
}
2016-10-29 00:46:05 +02:00
httpClient := c . HTTPClient
if httpClient == nil {
httpClient = http . DefaultClient
}
2015-02-05 01:37:43 +01:00
resp , err := httpClient . Do ( req )
if err != nil {
return nil , err
}
statusCode := resp . StatusCode
2016-03-21 20:08:47 +01:00
if statusCode >= 400 && statusCode <= 505 {
2015-02-05 01:37:43 +01:00
var respBody [ ] byte
2017-04-14 03:05:38 +02:00
respBody , err = readAndCloseBody ( resp . Body )
2015-02-05 01:37:43 +01:00
if err != nil {
return nil , err
}
2016-12-13 06:12:39 +01:00
requestID := resp . Header . Get ( "x-ms-request-id" )
2015-02-05 01:37:43 +01:00
if len ( respBody ) == 0 {
2016-12-13 06:12:39 +01:00
// no error in response body, might happen in HEAD requests
err = serviceErrFromStatusCode ( resp . StatusCode , resp . Status , requestID )
2015-02-05 01:37:43 +01:00
} else {
// response contains storage service error object, unmarshal
2016-12-13 06:12:39 +01:00
storageErr , errIn := serviceErrFromXML ( respBody , resp . StatusCode , requestID )
2015-02-05 01:37:43 +01:00
if err != nil { // error unmarshaling the error response
err = errIn
}
err = storageErr
}
return & storageResponse {
statusCode : resp . StatusCode ,
headers : resp . Header ,
body : ioutil . NopCloser ( bytes . NewReader ( respBody ) ) , /* restore the body */
} , err
}
return & storageResponse {
statusCode : resp . StatusCode ,
headers : resp . Header ,
body : resp . Body } , nil
}
2017-04-14 03:05:38 +02:00
func ( c Client ) execInternalJSON ( verb , url string , headers map [ string ] string , body io . Reader , auth authentication ) ( * odataResponse , error ) {
headers , err := c . addAuthorizationHeader ( verb , url , headers , auth )
if err != nil {
return nil , err
}
2016-10-29 00:46:05 +02:00
req , err := http . NewRequest ( verb , url , body )
for k , v := range headers {
req . Header . Add ( k , v )
}
httpClient := c . HTTPClient
if httpClient == nil {
httpClient = http . DefaultClient
}
resp , err := httpClient . Do ( req )
if err != nil {
return nil , err
}
respToRet := & odataResponse { }
respToRet . body = resp . Body
respToRet . statusCode = resp . StatusCode
respToRet . headers = resp . Header
statusCode := resp . StatusCode
if statusCode >= 400 && statusCode <= 505 {
var respBody [ ] byte
2017-04-14 03:05:38 +02:00
respBody , err = readAndCloseBody ( resp . Body )
2016-10-29 00:46:05 +02:00
if err != nil {
return nil , err
}
if len ( respBody ) == 0 {
2016-12-13 06:12:39 +01:00
// no error in response body, might happen in HEAD requests
err = serviceErrFromStatusCode ( resp . StatusCode , resp . Status , resp . Header . Get ( "x-ms-request-id" ) )
2016-10-29 00:46:05 +02:00
return respToRet , err
}
// try unmarshal as odata.error json
err = json . Unmarshal ( respBody , & respToRet . odata )
return respToRet , err
}
return respToRet , nil
}
2017-04-14 03:05:38 +02:00
func readAndCloseBody ( body io . ReadCloser ) ( [ ] byte , error ) {
defer body . Close ( )
out , err := ioutil . ReadAll ( body )
2015-02-05 01:37:43 +01:00
if err == io . EOF {
err = nil
}
return out , err
}
2015-06-12 00:30:18 +02:00
func serviceErrFromXML ( body [ ] byte , statusCode int , requestID string ) ( AzureStorageServiceError , error ) {
var storageErr AzureStorageServiceError
2015-02-05 01:37:43 +01:00
if err := xml . Unmarshal ( body , & storageErr ) ; err != nil {
return storageErr , err
}
storageErr . StatusCode = statusCode
2015-06-12 00:30:18 +02:00
storageErr . RequestID = requestID
2015-02-05 01:37:43 +01:00
return storageErr , nil
}
2016-12-13 06:12:39 +01:00
func serviceErrFromStatusCode ( code int , status string , requestID string ) AzureStorageServiceError {
return AzureStorageServiceError {
StatusCode : code ,
Code : status ,
RequestID : requestID ,
Message : "no response body was available for error status code" ,
}
}
2015-06-12 00:30:18 +02:00
func ( e AzureStorageServiceError ) Error ( ) string {
2016-02-08 23:29:21 +01:00
return fmt . Sprintf ( "storage: service returned error: StatusCode=%d, ErrorCode=%s, ErrorMessage=%s, RequestId=%s, QueryParameterName=%s, QueryParameterValue=%s" ,
e . StatusCode , e . Code , e . Message , e . RequestID , e . QueryParameterName , e . QueryParameterValue )
2015-06-12 00:30:18 +02:00
}
// checkRespCode returns UnexpectedStatusError if the given response code is not
// one of the allowed status codes; otherwise nil.
func checkRespCode ( respCode int , allowed [ ] int ) error {
for _ , v := range allowed {
if respCode == v {
return nil
}
}
return UnexpectedStatusCodeError { allowed , respCode }
2015-02-05 01:37:43 +01:00
}