-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathS3Object.cs
executable file
·121 lines (108 loc) · 3.62 KB
/
S3Object.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Affirma.ThreeSharp.Model;
using Affirma.ThreeSharp;
namespace Fr.Zhou.S3
{
public class S3Object : S3BaseType
{
string m_key;
public string Key { get { return m_key; } }
int m_size;
public int Size { get { return m_size; } }
string m_eTag;
public string ETag { get { return m_eTag.Substring(1, m_eTag.Length-2); } }
DateTime m_lastModified;
public DateTime LastModified { get { return m_lastModified; } }
string m_buckname;
/// <summary>
/// Construct an S3Object from the XML result of a GET request on a S3Bucket.
/// </summary>
internal S3Object(S3Connection connection, string BucketName, XmlNode node)
: base(connection)
{
m_key = node.SelectSingleS3String("s3:Key");
m_eTag = node.SelectSingleS3String("s3:ETag");
m_size = int.Parse(node.SelectSingleS3String("s3:Size"));
m_lastModified = node.SelectSingleS3Date("s3:LastModified");
m_buckname = BucketName;
}
internal S3Object(S3Connection connection, string BucketName, string Key)
: base(connection)
{
m_key = Key;
m_buckname = BucketName;
}
public void Get(string DistLocation)
{
ObjectGetResponse response = null;
try
{
ObjectGetRequest request = new ObjectGetRequest(m_buckname, m_key);
response = Service.ObjectGet(request);
response.StreamResponseToFile(DistLocation);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (response != null && response.DataStream != null)
response.DataStream.Close();
}
}
public void Upload(string SourceLocation)
{
ObjectAddResponse response = null;
ObjectAddRequest request = null;
try
{
request = new ObjectAddRequest(m_buckname, m_key);
if (m_key.EndsWith("/"))
{
//request.LoadStreamWithString("");
}
else
request.LoadStreamWithFile(SourceLocation);
response = Service.ObjectAdd(request);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (request != null && request.DataStream != null)
request.DataStream.Close();
if (response != null && response.DataStream != null)
response.DataStream.Close();
}
}
public void Delete()
{
ObjectDeleteResponse response = null;
try
{
ObjectDeleteRequest request = new ObjectDeleteRequest(m_buckname, m_key);
response = Service.ObjectDelete(request);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (response != null && response.DataStream != null)
response.DataStream.Close();
}
}
public override string ToString()
{
return m_key;
}
}
}