使用 .NET C# 存取 Google Storage

Google Storagegoogle I/O 2010發布的服務,目前僅提供測試服務,須取得邀請函才能使用。Google Storage的結構大致上是先建立budget,在budget下面可以建立目錄或放置檔案,檔案可以設定其存取權限是否為公開。目前 Google Storage 提供 Restful api 和 python 的官方程式存取方法,其他語言目前並不提供,不過網路上有熱心人士將它作成各語言的存取api,如C# 有SharpGs,開始用程式呼叫前須先取得Google Storage存取金鑰,在Google Storage首頁Manage your developer keys取得。

C#程式呼叫使用方法如以下簡介

using System;
using SharpGs;
using System.Collections.Generic;

namespace SharpGsDemo
{
class Program
{
//存取公私鑰 https://sandbox.google.com/storage/m/manage
//AuthKey 公鑰
//AuthSecret 私鑰
private const string AuthKey = @"GOOGP6666666666666666";
private const string AuthSecret = @"usdg/dfhEuiWfwzr4RGfgjdgj/sg+eVojTgDsg";

static void Main()
{
//建立連接端
var client = GoogleStorageFactory.Create(AuthKey, AuthSecret);

//隨機建立Bucket
for (var i = 0; i < 10; i++)
{
// Create a bucket1
client.CreateBucket("temp-bucket-" + new Random().Next());
}

//取得使用者所擁有的所有Bucket
foreach (var bucket in client.Buckets)
{
//讀取bucket名稱和建立時間
Console.WriteLine("{0} - {1}", bucket.Name, bucket.CreationDate);

//建立檔案
for (var i = 0; i < 5; i++)
{
bucket.AddObject("someobj/on" + new Random().Next(), new byte[] { 33, 77, 123, 34 }, "application/exe");
}

//讀取檔案
foreach (var o in bucket.Objects)
{
Console.WriteLine(" {0} - {1}", o.Key, o.Size);
o.Delete();
}

//刪除檔案
bucket.Delete();
}

Console.WriteLine("結束");
Console.ReadKey();
}
}
}

留言

這個網誌中的熱門文章

IIS 啟用HTTP Strict Transport Security (HSTS)

解決WCF(REST)在https出現檔案找不到錯誤

Azure Web Apps 讀取憑證