-
Notifications
You must be signed in to change notification settings - Fork 2
/
S3Bucket.cs
executable file
·143 lines (130 loc) · 4.9 KB
/
S3Bucket.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Affirma.ThreeSharp.Model;
using Affirma.ThreeSharp;
using System.Diagnostics;
using System.Net;
namespace Fr.Zhou.S3
{
public class S3Bucket : S3BaseType
{
string m_name;
string m_redirectUrl = null;
bool m_headed = false;
public string Name { get { return m_name; } }
internal S3Bucket(S3Connection connection, string name)
: base(connection)
{
m_name = name;
}
public bool Exists
{
get
{
try
{
BucketListRequest request = new BucketListRequest(m_name);
request.Method = "HEAD";
BucketListResponse response = Service.BucketList(request);
response.DataStream.Close();
if (response.StatusCode == System.Net.HttpStatusCode.TemporaryRedirect)
{
m_redirectUrl = response.Headers["Location"].ToString();
m_headed = true;
}
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
return false;
else
return true;
}
catch (Exception ex)
{
if (ex is WebException)
return false;
else
throw ex;
}
}
}
public IEnumerable<S3Object> Keys
{
get
{
BucketListRequest request = null;
BucketListResponse response = null;
try
{
if (!m_headed)
{
BucketListRequest testRequest = new BucketListRequest(m_name);
testRequest.Method = "HEAD";
BucketListResponse testResponse = Service.BucketList(testRequest);
testResponse.DataStream.Close();
if (testResponse.StatusCode == System.Net.HttpStatusCode.TemporaryRedirect)
{
m_redirectUrl = testResponse.Headers["Location"].ToString();
}
}
bool isTruncated = true;
string marker = string.Empty;
// The while-loop is here because S3 will only return a maximum of 1000 items at a time, so if the list
// was truncated, we need to make another call and get the rest
while (isTruncated)
{
request = new BucketListRequest(m_name);
request.RedirectUrl = m_redirectUrl;
if (!string.IsNullOrEmpty(marker))
{
request.QueryList.Add("marker", marker);
}
response = Service.BucketList(request);
XmlDocument xdoc = response.StreamResponseToXmlDocument();
Debug.WriteLine(xdoc);
XmlNode responseNode = xdoc.SelectSingleS3Node("/s3:ListBucketResult");
foreach (var objNode in responseNode.SelectS3Nodes("s3:Contents"))
{
S3Object obj = new S3Object(Connection, m_name, objNode);
yield return obj;
marker = obj.Key;
}
isTruncated = bool.Parse(responseNode.SelectSingleS3String("s3:IsTruncated"));
}
}
finally
{
if (response != null && response.DataStream != null) response.DataStream.Close();
}
}
}
public void Create()
{
try
{
BucketAddRequest request = new BucketAddRequest(m_name);
BucketAddResponse response = Service.BucketAdd(request);
}
catch (Exception ex)
{
throw ex;
}
}
public void DeleteDirectory(string DirectoryName)
{
foreach (var S3Obj in this.Keys)
{
if (S3Obj.Key.ToLower().StartsWith(DirectoryName.ToLower()))
{
S3Object S3O = new S3Object(Connection, m_name, S3Obj.Key);
S3O.Delete();
}
}
}
public override string ToString()
{
return m_name;
}
}
}