-
Notifications
You must be signed in to change notification settings - Fork 1
/
awss3helper.cs
131 lines (105 loc) · 4.83 KB
/
awss3helper.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
public class AWSStorageHelper
{
private static readonly string _awsAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"];
private static readonly string _awsSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"];
private static readonly string _bucketName = "xx";
public string determineAttachmentExtension(string ContentType)
{
string AttachmentExtension = ContentType.Substring(ContentType.IndexOf('/') + 1).ToLower();
if (AttachmentExtension == "jpeg")
{
AttachmentExtension = "jpg";
}
if (AttachmentExtension == "tiff")
{
AttachmentExtension = "tif";
}
return AttachmentExtension;
}
public string determineAttachmentExtensionByURL(string URL)
{
string AttachmentExtension = URL.Substring(URL.IndexOf('.') + 1).ToLower();
if (AttachmentExtension == "jpeg")
{
AttachmentExtension = "jpg";
}
if (AttachmentExtension == "tiff")
{
AttachmentExtension = "tif";
}
return AttachmentExtension;
}
public string determineAttachmentFileName(string Uri)
{
string AttachmentFileName = Uri.Substring(Uri.LastIndexOf('/') + 1).ToLower();
return AttachmentFileName;
}
public string UploadtoS3(string fileurl, string filetype, string folder, string prefix, string filename)
{
string returnString = "";
Guid newGuid = Guid.NewGuid();
var newGuidstr = newGuid.ToString();
string url = fileurl;
byte[] imageData;
using (WebClient client = new WebClient())
{
imageData = client.DownloadData(url);
}
try
{
IAmazonS3 client;
using (client = new AmazonS3Client(_awsAccessKey, _awsSecretKey, Amazon.RegionEndpoint.USEast1))
{
var request = new PutObjectRequest()
{
BucketName = _bucketName,
CannedACL = S3CannedACL.PublicRead,
Key = string.Format(folder + "/{0}", prefix + "-" + newGuidstr.Substring(0, 4) + "-" + filename + "." + filetype)
};
using (var ms = new MemoryStream(imageData))
{
request.InputStream = ms;
client.PutObject(request);
returnString = "https://s3.amazonaws.com/xx/" + request.Key;
}
}
}
catch (Exception ex)
{
returnString = ex.Message;
}
return returnString;
}
public ResourceData UploadtoAWSByte(byte[] imageData, string filename )
{
var ResourceData = new ResourceData();
string returnString = "";
Guid newGuid = Guid.NewGuid();
var newGuidstr = newGuid.ToString();
try
{
IAmazonS3 client;
using (client = new AmazonS3Client(_awsAccessKey, _awsSecretKey, Amazon.RegionEndpoint.USEast1))
{
var request = new PutObjectRequest()
{
BucketName = _bucketName,
CannedACL = S3CannedACL.PublicRead,
Key = string.Format("xxxxx" + "/{0}", "xxx" + "-" + newGuidstr.Substring(0, 4) + "-" + filename + ".pdf")
};
using (var ms = new MemoryStream(imageData))
{
request.InputStream = ms;
client.PutObject(request);
returnString = "https://s3.amazonaws.com/xx/" + request.Key;
}
}
}
catch (Exception ex)
{
returnString = ex.Message;
}
ResourceData.ResourceURL2Primary = returnString;
return ResourceData;
}
}