-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBlobService.cs
144 lines (119 loc) · 6.21 KB
/
BlobService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using RESTClient;
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
namespace Azure.StorageServices {
public class BlobService {
private StorageServiceClient client;
public BlobService(StorageServiceClient client) {
this.client = client;
}
/// <summary>
/// Lists all of the containers in a storage account.
/// </summary>
/// <returns>The containers.</returns>
/// <param name="">.</param>
public IEnumerator ListContainers(Action<IRestResponse<ContainerResults>> callback) {
Dictionary<string, string> queryParams = new Dictionary<string, string>();
queryParams.Add("comp", "list");
queryParams.Add("restype", ResType.container.ToString());
StorageRequest request = Auth.CreateAuthorizedStorageRequest(client, Method.GET, "", queryParams);
yield return request.Send();
request.ParseXML<ContainerResults>(callback);
}
/// <summary>
/// Lists all of the blobs in a container.
/// </summary>
/// <returns>The containers.</returns>
/// <param name="">.</param>
public IEnumerator ListBlobs(Action<IRestResponse<BlobResults>> callback, string resourcePath = "") {
Dictionary<string, string> queryParams = new Dictionary<string, string>();
queryParams.Add("comp", "list");
queryParams.Add("restype", ResType.container.ToString());
StorageRequest request = Auth.CreateAuthorizedStorageRequest(client, Method.GET, resourcePath, queryParams);
yield return request.Send();
request.ParseXML<BlobResults>(callback);
}
#region Download blobs
public IEnumerator GetTextBlob(Action<RestResponse> callback, string resourcePath = "") {
StorageRequest request = Auth.GetAuthorizedStorageRequest(client, resourcePath);
yield return request.Send();
request.GetText(callback);
}
public IEnumerator GetJsonBlob<T>(Action<IRestResponse<T>> callback, string resourcePath = "") {
StorageRequest request = Auth.GetAuthorizedStorageRequest(client, resourcePath);
yield return request.Send();
request.ParseJson(callback);
}
public IEnumerator GetJsonArrayBlob<T>(Action<IRestResponse<T[]>> callback, string resourcePath = "") {
StorageRequest request = Auth.GetAuthorizedStorageRequest(client, resourcePath);
yield return request.Send();
request.ParseJsonArray(callback);
}
public IEnumerator GetXmlBlob<T>(Action<IRestResponse<T>> callback, string resourcePath = "") {
StorageRequest request = Auth.GetAuthorizedStorageRequest(client, resourcePath);
yield return request.Send();
request.ParseXML<T>(callback);
}
public IEnumerator GetImageBlob(Action<IRestResponse<Texture>> callback, string resourcePath = "") {
StorageRequest request = Auth.GetAuthorizedStorageRequestTexture(client, resourcePath);
yield return request.Send();
request.GetTexture(callback);
}
public IEnumerator GetAudioBlob(Action<IRestResponse<AudioClip>> callback, string resourcePath = "") {
StorageRequest request = Auth.GetAuthorizedStorageRequestAudioClip(client, resourcePath);
yield return request.Send();
request.GetAudioClip(callback);
}
public IEnumerator GetAssetBundle(Action<IRestResponse<AssetBundle>> callback, string resourcePath = "") {
StorageRequest request = Auth.GetAuthorizedStorageRequestAssetBundle(client, resourcePath);
yield return request.Send();
request.GetAssetBundle(callback);
}
public IEnumerator GetBlob(Action<IRestResponse<byte[]>> callback, string resourcePath = "") {
StorageRequest request = Auth.GetAuthorizedStorageRequest(client, resourcePath);
yield return request.Send();
request.GetBytes(callback);
}
#endregion
#region Upload blobs
public IEnumerator PutTextBlob(Action<RestResponse> callback, string text, string resourcePath, string filename, string contentType = "text/plain; charset=UTF-8") {
byte[] bytes = Encoding.UTF8.GetBytes(text);
return PutBlob(callback, bytes, resourcePath, filename, contentType);
}
public IEnumerator PutImageBlob(Action<RestResponse> callback, byte[] bytes, string resourcePath, string filename, string contentType = "image/png") {
return PutBlob(callback, bytes, resourcePath, filename, contentType);
}
public IEnumerator PutAudioBlob(Action<RestResponse> callback, byte[] bytes, string resourcePath, string filename, string contentType = "audio/wav") {
return PutBlob(callback, bytes, resourcePath, filename, contentType);
}
public IEnumerator PutAssetBundle(Action<RestResponse> callback, byte[] bytes, string resourcePath, string filename, string contentType = "application/octet-stream") {
return PutBlob(callback, bytes, resourcePath, filename, contentType);
}
public IEnumerator PutBlob(Action<RestResponse> callback, byte[] bytes, string resourcePath, string filename, string contentType, Method method = Method.PUT) {
int contentLength = bytes.Length; // TODO: check size is ok?
Dictionary<string, string> headers = new Dictionary<string, string>();
string file = Path.GetFileName(filename);
headers.Add("Content-Type", contentType);
headers.Add("x-ms-blob-content-disposition", string.Format("attachment; filename=\"{0}\"", file));
headers.Add("x-ms-blob-type", "BlockBlob");
string filePath = resourcePath.Length > 0 ? resourcePath + "/" + file : file;
StorageRequest request = Auth.CreateAuthorizedStorageRequest(client, method, filePath, null, headers, contentLength);
request.AddBody(bytes, contentType);
yield return request.Send();
request.Result(callback);
}
#endregion
public IEnumerator DeleteBlob(Action<RestResponse> callback, string resourcePath, string filename) {
string filePath = resourcePath.Length > 0 ? resourcePath + "/" + filename : filename;
StorageRequest request = Auth.CreateAuthorizedStorageRequest(client, Method.DELETE, filePath);
yield return request.Send();
request.Result(callback);
}
}
}