I try to upload an image from my WCF service to Amazon S3 web server. I have the Amazon S3 code that is working in a web project and my image upload method that is uploading image in Uploded\test.jpg in WCF service. I am not sure how I can use Amazon S3 code working with WCF service. 1st I don't know how to put Amazon credential in the web confing when I add these line of code inside the it is not uploading:
<appSettings>
<add key="AWSAccessKey" value="myaccessKey"/>
<add key="AWSSecretKey" value="MySecretKey"/>
</appSettings>
and this is my method to upload to the WCF server I guess I have to add AWS code when I said //AWS here:
[WebInvoke(UriTemplate = "UploadImage", Method = "POST")]
Stream UploadImage(Stream request)
{
Stream requestTest = request;
StreamWriter sw = null;
string logpath = HttpContext.Current.Server.MapPath("Uploded\\logtest.txt");
logpath = logpath.Replace("SSGTrnService\\", "");
HttpMultipartParser parser = new HttpMultipartParser(request, "file");
string filePath = "";
string passed = parser._content;
string sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
sw = new StreamWriter(logpath);
sw.Flush();
if (parser.Success)
{
// Save the file somewhere
//File.WriteAllBytes(FILE_PATH + title + FILE_EXT, parser.FileContents);
// Save the file
//SaveFile( mtp.Filename, mtp.ContentType, mtp.FileContents);
FileStream fileStream = null;
BinaryWriter writer = null;
try
{
filePath = HttpContext.Current.Server.MapPath("Uploded\\test.jpg"); // BuildFilePath(strFileName, true);
filePath = filePath.Replace("SSGTrnService\\", "");
fileStream = new FileStream(filePath, FileMode.Create);
fileStream.Write(parser.FileContents, 0, parser.FileContents.Length);
// return filePath;
}
catch (Exception ex)
{
return "Error: " + ex.Message;
}
finally
{
if (fileStream != null)
fileStream.Close();
if (writer != null)
writer.Close();
//AWS Code
}
}
//
// returning text for html DOM
//
string text = "Image uploaded: " + parser.Filename + " / " + parser.ContentType + filePath + passed;
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
MemoryStream ms = new MemoryStream(encoding.GetBytes(text));
WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
return ms;
}
Any Guide in calling Amazon S3 from WCF service would be great.
There is a amazon dll that you would need to reference (AWSSDK.dll) and then use the below lines of code:
var transferUtility = new TransferUtility(accessKey, secretKey);
var bucketName = "Files";
transferUtility.Upload(filePath, bucketName, Guid.NewGuid().ToString());
NOTE: Please make sure that the Amazon S3 bucket "Files" exists. Else you need to check if the bucket exists and then perform the upload method call. Hope that helps.