-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcsManager.cs
73 lines (53 loc) · 1.81 KB
/
gcsManager.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Storage.V1;
namespace COLT
{
public class gcsManager
{
private string _bucketName;
private string _projectid;
private StorageClient _gcsClient;
private GoogleCredential _cred;
private string _jsonCred;
public gcsManager(string cloudCredentialJson, string bucketName, string projectid)
{
_bucketName = bucketName;
_projectid = projectid;
_jsonCred = cloudCredentialJson;
_cred = GoogleCredential.FromJson(cloudCredentialJson);
_gcsClient = StorageClient.Create(_cred);
}
public string GeneratePreSignedURL(int durationInMinutes, string key)
{
string urlString = "";
var cred = _cred.UnderlyingCredential as ServiceAccountCredential;
UrlSigner urlSigner = UrlSigner.FromServiceAccountCredential(cred);
urlString = urlSigner.Sign(
_bucketName,
key,
TimeSpan.FromMinutes(durationInMinutes),
HttpMethod.Get);
return urlString;
}
public async Task UploadFileAsync(byte[] bytes, string name)
{
await _gcsClient.UploadObjectAsync(_bucketName, name, "application/octet-stream", new MemoryStream(bytes));
}
public List<string> GetFileList(string prefix)
{
List<string> objects = new List<string>();
foreach (var obj in _gcsClient.ListObjects(_bucketName, prefix))
{
objects.Add(obj.Name);
}
return objects;
}
}
}