-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
97 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
Backup2Cloud/Worker/Uploader/GoogleCloudStorageUploader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Google.Apis.Auth.OAuth2; | ||
using Google.Cloud.Storage.V1; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Backup2Cloud.Worker.Uploader | ||
{ | ||
/// <summary> | ||
/// 谷歌云存储上传实现类 | ||
/// </summary> | ||
[ProviderName("google")] | ||
public class GoogleCloudStorageUploader : IUploader | ||
{ | ||
/// <summary> | ||
/// 服务账号密钥文件位置 | ||
/// </summary> | ||
public string jsonKeyFile; | ||
/// <summary> | ||
/// 存储空间名 | ||
/// </summary> | ||
public string bucket; | ||
/// <summary> | ||
/// 文件在Bucket下的路径,作为上传路径前缀。 | ||
/// </summary> | ||
public string path; | ||
/// <summary> | ||
/// 提示信息 | ||
/// </summary> | ||
public string Tips | ||
{ | ||
get | ||
{ | ||
return "jsonKeyFile:服务账号密钥文件位置" + | ||
"bucket:存储空间名;" + | ||
"path:文件在Bucket/桶下的路径前缀,例如\"data/some\",最终会生成类似\"data/some201809092054.zip\"之类的文件"; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 获取示例实例。 | ||
/// </summary> | ||
/// <returns>示例配置实例</returns> | ||
public IUploader GetExample() | ||
{ | ||
return new GoogleCloudStorageUploader() | ||
{ | ||
jsonKeyFile = "/conf/xxx.json", | ||
bucket = "backup", | ||
path = "data/some" | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// 上传指定文件。 | ||
/// </summary> | ||
/// <param name="file">待上传的文件</param> | ||
/// <param name="suffix">文件在云空间里的后缀</param> | ||
/// <exception cref="Exception"/> | ||
public Task Upload(string file, string suffix) | ||
{ | ||
using (FileStream stream = new FileStream(file, FileMode.Open)) | ||
{ | ||
GoogleCredential credential = GoogleCredential.FromFile(jsonKeyFile); | ||
var client = StorageClient.Create(credential); | ||
client.UploadObject(bucket, path + suffix, "application/x-zip-compressed", stream); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
version: 0.0.6.1.{build} | ||
version: 0.0.7.0.{build} | ||
image: Visual Studio 2017 | ||
shallow_clone: true | ||
build_script: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters