Skip to content

Commit

Permalink
Include property response headers in get blob operation too (Azure#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcardosos authored and marstr committed Apr 26, 2017
1 parent 0d6b7d4 commit 1268659
Show file tree
Hide file tree
Showing 117 changed files with 44 additions and 22 deletions.
59 changes: 38 additions & 21 deletions storage/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ func (b *Blob) Get(options *GetBlobOptions) (io.ReadCloser, error) {
if err := checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil {
return nil, err
}
if err := b.writePropoerties(resp.headers); err != nil {
return resp.body, err
}
return resp.body, nil
}

Expand All @@ -209,6 +212,9 @@ func (b *Blob) GetRange(options *GetBlobRangeOptions) (io.ReadCloser, error) {
if err := checkRespCode(resp.statusCode, []int{http.StatusPartialContent}); err != nil {
return nil, err
}
if err := b.writePropoerties(resp.headers); err != nil {
return resp.body, err
}
return resp.body, nil
}

Expand Down Expand Up @@ -316,9 +322,14 @@ func (b *Blob) GetProperties(options *GetBlobPropertiesOptions) error {
if err = checkRespCode(resp.statusCode, []int{http.StatusOK}); err != nil {
return err
}
return b.writePropoerties(resp.headers)
}

func (b *Blob) writePropoerties(h http.Header) error {
var err error

var contentLength int64
contentLengthStr := resp.headers.Get("Content-Length")
contentLengthStr := h.Get("Content-Length")
if contentLengthStr != "" {
contentLength, err = strconv.ParseInt(contentLengthStr, 0, 64)
if err != nil {
Expand All @@ -327,44 +338,46 @@ func (b *Blob) GetProperties(options *GetBlobPropertiesOptions) error {
}

var sequenceNum int64
sequenceNumStr := resp.headers.Get("x-ms-blob-sequence-number")
sequenceNumStr := h.Get("x-ms-blob-sequence-number")
if sequenceNumStr != "" {
sequenceNum, err = strconv.ParseInt(sequenceNumStr, 0, 64)
if err != nil {
return err
}
}

lastModified, err := getTimeFromHeaders(resp.headers, "Last-Modified")
lastModified, err := getTimeFromHeaders(h, "Last-Modified")
if err != nil {
return err
}

copyCompletionTime, err := getTimeFromHeaders(resp.headers, "x-ms-copy-completion-time")
copyCompletionTime, err := getTimeFromHeaders(h, "x-ms-copy-completion-time")
if err != nil {
return err
}

b.Properties = BlobProperties{
LastModified: TimeRFC1123(*lastModified),
Etag: resp.headers.Get("Etag"),
ContentMD5: resp.headers.Get("Content-MD5"),
Etag: h.Get("Etag"),
ContentMD5: h.Get("Content-MD5"),
ContentLength: contentLength,
ContentEncoding: resp.headers.Get("Content-Encoding"),
ContentType: resp.headers.Get("Content-Type"),
CacheControl: resp.headers.Get("Cache-Control"),
ContentLanguage: resp.headers.Get("Content-Language"),
ContentEncoding: h.Get("Content-Encoding"),
ContentType: h.Get("Content-Type"),
ContentDisposition: h.Get("Content-Disposition"),
CacheControl: h.Get("Cache-Control"),
ContentLanguage: h.Get("Content-Language"),
SequenceNumber: sequenceNum,
CopyCompletionTime: TimeRFC1123(*copyCompletionTime),
CopyStatusDescription: resp.headers.Get("x-ms-copy-status-description"),
CopyID: resp.headers.Get("x-ms-copy-id"),
CopyProgress: resp.headers.Get("x-ms-copy-progress"),
CopySource: resp.headers.Get("x-ms-copy-source"),
CopyStatus: resp.headers.Get("x-ms-copy-status"),
BlobType: BlobType(resp.headers.Get("x-ms-blob-type")),
LeaseStatus: resp.headers.Get("x-ms-lease-status"),
LeaseState: resp.headers.Get("x-ms-lease-state"),
}
CopyStatusDescription: h.Get("x-ms-copy-status-description"),
CopyID: h.Get("x-ms-copy-id"),
CopyProgress: h.Get("x-ms-copy-progress"),
CopySource: h.Get("x-ms-copy-source"),
CopyStatus: h.Get("x-ms-copy-status"),
BlobType: BlobType(h.Get("x-ms-blob-type")),
LeaseStatus: h.Get("x-ms-lease-status"),
LeaseState: h.Get("x-ms-lease-state"),
}
b.writeMetadata(h)
return nil
}

Expand Down Expand Up @@ -506,8 +519,13 @@ func (b *Blob) GetMetadata(options *GetBlobMetadataOptions) error {
return err
}

b.writeMetadata(resp.headers)
return nil
}

func (b *Blob) writeMetadata(h http.Header) {
metadata := make(map[string]string)
for k, v := range resp.headers {
for k, v := range h {
// Can't trust CanonicalHeaderKey() to munge case
// reliably. "_" is allowed in identifiers:
// https://msdn.microsoft.com/en-us/library/azure/dd179414.aspx
Expand All @@ -527,7 +545,6 @@ func (b *Blob) GetMetadata(options *GetBlobMetadataOptions) error {
}

b.Metadata = BlobMetadata(metadata)
return nil
}

// DeleteBlobOptions includes the options for a delete blob operation
Expand Down
7 changes: 6 additions & 1 deletion storage/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,11 @@ func (s *StorageBlobSuite) TestSetMetadataWithExtraHeaders(c *chk.C) {

c.Assert(b.putSingleBlockBlob([]byte("Hello!")), chk.IsNil)

b.Metadata = BlobMetadata{
meta := BlobMetadata{
"lol": "rofl",
"rofl_baz": "waz qux",
}
b.Metadata = meta

options := SetBlobMetadataOptions{
IfMatch: "incorrect-etag",
Expand All @@ -272,6 +273,7 @@ func (s *StorageBlobSuite) TestSetMetadataWithExtraHeaders(c *chk.C) {

// Set with matching If-Match in extra headers should succeed
options.IfMatch = b.Properties.Etag
b.Metadata = meta
err = b.SetMetadata(&options)
c.Assert(err, chk.IsNil)
}
Expand Down Expand Up @@ -452,6 +454,9 @@ func (s *StorageBlobSuite) TestGetBlobRange(c *chk.C) {

str := string(blobBody)
c.Assert(str, chk.Equals, r.expected)

// Was content lenght properly updated...?
c.Assert(b.Properties.ContentLength, chk.Equals, int64(len(r.expected)))
}
}

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 1268659

Please sign in to comment.