我尝试将图像从我的WCF服务上传到Amazon S3 Web服务器。我有一个在Web项目中工作的Amazon S3代码和我的图像上传方法,它在WCF服务中的Uploded \ test.jpg中上传图像。我不确定如何使用与WCF服务一起使用的Amazon S3代码。第一,我不知道如何在网络中加入亚马逊凭证,当我在其中添加这些代码时,不会上传:
<appSettings>
<add key="AWSAccessKey" value="myaccessKey"/>
<add key="AWSSecretKey" value="MySecretKey"/>
</appSettings>
这是我上传到WCF服务器的方法我想我在这里说// AWS时必须添加AWS代码:
[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;
}
从WCF服务调用Amazon S3的任何指南都会很棒。
答案 0 :(得分:0)
您需要引用一个amazon dll(AWSSDK.dll),然后使用以下代码行:
var transferUtility = new TransferUtility(accessKey, secretKey);
var bucketName = "Files";
transferUtility.Upload(filePath, bucketName, Guid.NewGuid().ToString());
注意:请确保存在Amazon S3存储桶“文件”。否则,您需要检查存储桶是否存在,然后执行上载方法调用。希望有所帮助。