0

我尝试将图像从我的 WCF 服务上传到 Amazon S3 Web 服务器。我有在 Web 项目中工作的 Amazon S3 代码和在 WCF 服务中的 Uploded\test.jpg 中上传图像的图像上传方法。我不确定如何将 Amazon S3 代码与 WCF 服务一起使用。第一,当我在未上传的情况下添加这些代码行时,我不知道如何将亚马逊凭证放入网络配置中:

    <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 的任何指南都会很棒。

4

1 回答 1

0

您需要引用一个亚马逊 dll (AWSSDK.dll),然后使用以下代码行:

var transferUtility = new TransferUtility(accessKey, secretKey);
var bucketName = "Files";
transferUtility.Upload(filePath, bucketName, Guid.NewGuid().ToString());

注意:请确保 Amazon S3 存储桶“文件”存在。否则,您需要检查存储桶是否存在,然后执行上传方法调用。希望有帮助。

于 2013-05-23T11:22:34.750 回答