Skip to content

Commit 1c96db8

Browse files
committed
getobject增加返回metadata。
1 parent 46014c5 commit 1c96db8

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/Api/Object/Get/GetObjectCommand.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
using System.Net.Http;
1+
using System;
2+
using System.Diagnostics;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Net.Http;
26
using System.Threading.Tasks;
37
using Cuiliang.AliyunOssSdk.Api.Base;
8+
using Cuiliang.AliyunOssSdk.Api.Common.Consts;
49
using Cuiliang.AliyunOssSdk.Entites;
510
using Cuiliang.AliyunOssSdk.Request;
11+
using Cuiliang.AliyunOssSdk.Utility;
612

713
namespace Cuiliang.AliyunOssSdk.Api.Object.Get
814
{
@@ -43,12 +49,47 @@ public override async Task<OssResult<GetObjectResult>> ParseResultAsync(HttpResp
4349
var result = new GetObjectResult();
4450
result.Headers = response.Headers;
4551
result.Content = response.Content;
52+
result.Metadata = Deserialize(response);
4653

4754
return new OssResult<GetObjectResult>()
4855
{
4956
IsSuccess = true,
5057
SuccessResult = result
5158
};
5259
}
60+
61+
private ObjectMetadata Deserialize(HttpResponseMessage response)
62+
{
63+
64+
var metadata = new ObjectMetadata();
65+
foreach (var header in response.Headers)
66+
{
67+
if (header.Key.StartsWith(OssHeaders.OssUserMetaPrefix, false, CultureInfo.InvariantCulture))
68+
{
69+
// The key of user in the metadata should not contain the prefix.
70+
metadata.UserMetadata.Add(header.Key.Substring(OssHeaders.OssUserMetaPrefix.Length),
71+
header.Value.FirstOrDefault());
72+
}
73+
else if (string.Equals(header.Key, HttpHeaders.ContentLength, StringComparison.InvariantCultureIgnoreCase))
74+
{
75+
// Content-Length. Parse should not fail.
76+
metadata.ContentLength = long.Parse(header.Value.FirstOrDefault(), CultureInfo.InvariantCulture);
77+
}
78+
else if (string.Equals(header.Key, HttpHeaders.ETag, StringComparison.InvariantCultureIgnoreCase))
79+
{
80+
metadata.ETag = OssUtils.TrimETag(header.Value.FirstOrDefault());
81+
}
82+
else if (string.Equals(header.Key, HttpHeaders.LastModified, StringComparison.InvariantCultureIgnoreCase))
83+
{
84+
metadata.LastModified = DateUtils.ParseRfc822Date(header.Value.FirstOrDefault());
85+
}
86+
else
87+
{
88+
// Treat the other headers just as strings.
89+
metadata.AddHeader(header.Key, header.Value);
90+
}
91+
}
92+
return metadata;
93+
}
5394
}
5495
}

src/Api/Object/Get/GetObjectResult.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Net.Http;
22
using System.Net.Http.Headers;
3+
using Cuiliang.AliyunOssSdk.Entites;
34

45
namespace Cuiliang.AliyunOssSdk.Api.Object.Get
56
{
@@ -9,6 +10,6 @@ public class GetObjectResult
910

1011
public HttpContent Content { get; set; }
1112

12-
13+
public ObjectMetadata Metadata { get; set; }
1314
}
1415
}

src/Utility/OssUtils.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,5 +354,10 @@ internal static string JoinETag(IEnumerable<string> etags)
354354
// if (string.IsNullOrEmpty(accessKeySecret))
355355
// throw new ArgumentException(Resources.ExceptionIfArgumentStringIsNullOrEmpty, "accessKeySecret");
356356
//}
357+
358+
public static string TrimETag(string eTag)
359+
{
360+
return eTag != null ? eTag.Trim('\"') : null;
361+
}
357362
}
358363
}

0 commit comments

Comments
 (0)