Skip to content

Commit

Permalink
fix #2 支持Google云存储。
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjusss committed Sep 16, 2018
1 parent 3820884 commit b1dfe2e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
7 changes: 4 additions & 3 deletions Backup2Cloud/Backup2Cloud.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<TargetFramework>netcoreapp2.0</TargetFramework>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<NeutralLanguage></NeutralLanguage>
<AssemblyVersion>0.0.6.1</AssemblyVersion>
<FileVersion>0.0.6.1</FileVersion>
<Version>0.0.6.1</Version>
<AssemblyVersion>0.0.7.0</AssemblyVersion>
<FileVersion>0.0.7.0</FileVersion>
<Version>0.0.7</Version>
<Authors>sanjusss</Authors>
<Company />
<Description>定时将服务器上的文件备份到云存储。</Description>
Expand All @@ -22,6 +22,7 @@
<PackageReference Include="AWSSDK.S3" Version="3.3.23.2" />
<PackageReference Include="BceSdkDotNetCore" Version="1.0.2.911" />
<PackageReference Include="CommandLineParser" Version="2.3.0" />
<PackageReference Include="Google.Cloud.Storage.V1" Version="2.2.0" />
<PackageReference Include="HuaweiCloud.SDK.OBS" Version="3.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="QCloudSDK.COS" Version="1.0.0" />
Expand Down
74 changes: 74 additions & 0 deletions Backup2Cloud/Worker/Uploader/GoogleCloudStorageUploader.cs
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;
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
|[青云](https://www.qingcloud.com)|[对象存储](https://www.qingcloud.com/products/qingstor/)|![支持](https://img.shields.io/badge/support-Yes-green.svg)|
|[金山云](https://www.ksyun.com)|[对象存储](https://www.ksyun.com/post/product/KS3)|![不支持](https://img.shields.io/badge/support-No-red.svg)|仅限企业用户|
|[Azure](https://www.azure.cn/zh-cn/)|[Blob 存储](https://www.azure.cn/zh-cn/home/features/storage/blobs/)|![即将到来](https://img.shields.io/badge/support-Future-yellow.svg)|
|[Google Cloud](https://cloud.google.com)|[Google Cloud Storage](https://cloud.google.com/storage/)|![即将到来](https://img.shields.io/badge/support-Future-yellow.svg)|
|[Google Cloud](https://cloud.google.com)|[Google Cloud Storage](https://cloud.google.com/storage/)|![支持](https://img.shields.io/badge/support-Yes-green.svg)|

# 支持的数据源

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
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:
Expand Down
17 changes: 17 additions & 0 deletions example.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@
},
"Tips": "name:任务名称;provider:上传服务提供商;path:需要备份的文件夹或文件在本地的路径;crontab:启动备份的时间集合,可以参考http://cron.qqe2.com/,使用时需要注意时区;command:打包备份文件夹或文件之前额外执行的命令,例如备份数据库等,使用docker容器时须注意执行环境,可以为空;commandArgs:打包备份文件夹或文件之前额外执行的命令的参数,,可以为空;uploader:上传设置"
},
{
"name": "上传到 google",
"provider": "google",
"path": "/data",
"crontab": [
"0,30 * * * * ?"
],
"command": "echo",
"commandArgs": "Hello World!",
"uploader": {
"jsonKeyFile": "/conf/xxx.json",
"bucket": "backup",
"path": "data/some",
"Tips": "jsonKeyFile:服务账号密钥文件位置bucket:存储空间名;path:文件在Bucket/桶下的路径前缀,例如\"data/some\",最终会生成类似\"data/some201809092054.zip\"之类的文件"
},
"Tips": "name:任务名称;provider:上传服务提供商;path:需要备份的文件夹或文件在本地的路径;crontab:启动备份的时间集合,可以参考http://cron.qqe2.com/,使用时需要注意时区;command:打包备份文件夹或文件之前额外执行的命令,例如备份数据库等,使用docker容器时须注意执行环境,可以为空;commandArgs:打包备份文件夹或文件之前额外执行的命令的参数,,可以为空;uploader:上传设置"
},
{
"name": "上传到 huawei",
"provider": "huawei",
Expand Down

0 comments on commit b1dfe2e

Please sign in to comment.