-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
78 lines (68 loc) · 2.81 KB
/
Program.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
using System;
using System.IO;
using System.Text;
using SharpGs;
namespace SharpGsDemo
{
class Program
{
// Google keys pair - key & secret
private const string AuthKey = @"put yours here";
private const string AuthSecret = @"put yours here";
static void Main()
{
var client = GoogleStorageFactory.Create(AuthKey, AuthSecret);
//--- Proxy usage (implemented by community user Fabrizio)
//IWebProxy proxy = HttpWebRequest.DefaultWebProxy;
//proxy.Credentials = CredentialCache.DefaultCredentials;
//client.WebProxy = proxy;
for (var i = 0; i < 2; i++)
{
// Create a bucket
var name = "temp-bucket-" + new Random().Next();
client.CreateBucket(name);
}
// Fetching all buckets of user
foreach (var bucket in client.Buckets)
{
Console.WriteLine("{0} - {1}", bucket.Name, bucket.CreationDate);
// Simple buffer content
bucket.AddObject("someobj/on" + new Random().Next(), Encoding.UTF8.GetBytes("Simple text"), "text/plain");
// Streamed content (stream will be closed at the end)
bucket.AddObject("someobj/stream-on" + new Random().Next(), GetStreamedString("Stream me!!!"), "text/plain", true);
foreach (var o in bucket.Objects)
{
Console.WriteLine(" {0} - {1}", o.Key, o.Size);
if (o.Key.StartsWith("someobj/stream-on"))
{
// Get streamed content
using (var ms = new MemoryStream())
{
o.Retrieve(ms);
ms.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(ms))
{
Console.WriteLine(" {0}", reader.ReadToEnd());
}
}
}
else
{
// Get simple content
Console.WriteLine(" {0}", Encoding.UTF8.GetString(o.Retrieve().Content));
}
o.Delete();
}
// Delete bucket
if (bucket.Name.StartsWith("temp-bucket-"))
bucket.Delete();
}
Console.WriteLine("Finished");
Console.ReadKey();
}
static Stream GetStreamedString(string data)
{
return new MemoryStream(Encoding.UTF8.GetBytes(data));
}
}
}