如何在.net MVC 4.5中使用C#在亚马逊S3上传图像?

时间:2017-12-11 06:59:51

标签: amazon-web-services amazon-s3

我想在亚马逊S3服务器上上传和管理图片。是否有任何c#插件已经可用?我正在使用.net MVC 4.5框架。

我试过以下链接 Upload an Object Using the AWS SDK for .NET

但它没有用。

1 个答案:

答案 0 :(得分:1)

我创建了以下类来专门上传S3服务器上的图像。您可以对输入类型和方法的返回类型进行一些更改,以使其适合您。

来源:

https://askgif.com/blog/147/upload-an-image-on-amazon-s3-using-c-in-net-mvc-4-5-core-2-1/

你只需要提供IAM的AccessKey和SecretKey。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Web;
using Amazon.S3;
using Amazon.S3.Model;
using urNotice.Common.Infrastructure.Common.Config;
using urNotice.Common.Infrastructure.Model.urNoticeModel.ResponseWrapper;
using urNotice.Services.ErrorLogger;

namespace urNotice.Services.UploadImageService
{
  public class S3ImageUploadServices
  {

private static readonly ILogger Logger = new Logger(Convert.ToString(MethodBase.GetCurrentMethod().DeclaringType));        
private static readonly string _awsAccessKey = AwsConfig._awsAccessKey;
private static readonly string _awsSecretKey = AwsConfig._awsSecretKey;
private static readonly string _bucketName = AwsConfig._bucketName;
private static readonly string _amazonS3PublicUrl = AwsConfig._amazonS3PublicUrl;

public static ImgurImageResponse UploadMultipleImagesToImgur(IEnumerable<HttpPostedFileBase> files, string albumid)
{


    var fileName = Guid.NewGuid().ToString();
    var path = albumid + "/" + fileName;
    var imgurImage = new ImgurImageResponse();
    foreach (HttpPostedFileBase file in files)
    {

        try
        {
            path = path +"."+ file.FileName.Split('.').Last();
            IAmazonS3 client;
            Stream inputSteram = ResizeImageFile(file.InputStream, 1024);
            using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(_awsAccessKey, _awsSecretKey, Amazon.RegionEndpoint.APSoutheast1))
            {
                var request = new PutObjectRequest()
                {
                    BucketName = _bucketName,
                    CannedACL = S3CannedACL.PublicRead,//PERMISSION TO FILE PUBLIC ACCESIBLE
                    Key = string.Format(path),
                    InputStream = inputSteram//SEND THE FILE STREAM
                };

                client.PutObject(request);
            }
        }
        catch (Exception ex)
        {

            Logger.Error("Exception Occured while uploading to Amazon S3 : "+ex, ex);
        }

            imgurImage.data = new imgurData();

            imgurImage.data.link = _amazonS3PublicUrl+"/"+_bucketName+"/"+path;
            imgurImage.data.link_s = _amazonS3PublicUrl + "/" + _bucketName + "/" + path;
            imgurImage.data.link_m = _amazonS3PublicUrl + "/" + _bucketName + "/" + path;
            imgurImage.data.link_l = _amazonS3PublicUrl + "/" + _bucketName + "/" + path;
            imgurImage.data.copyText = "";

    }
    return imgurImage;
}

public static ImgurImageResponse UploadSingleImageToImgur(HttpPostedFileBase file, string albumid)
{


    var fileName = Guid.NewGuid().ToString();
    var path = albumid + "/" + fileName;
    var imgurImage = new ImgurImageResponse();

        try
        {
            path = path + "." + file.FileName.Split('.').Last();
            IAmazonS3 client;
            Stream inputSteram = null;

            if (file.FileName.Split('.')[1] == "gif")
            {
                inputSteram = file.InputStream;
            }
            else
            {
                inputSteram = ResizeImageFile(file.InputStream, 1024);
            }

            using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(_awsAccessKey, _awsSecretKey, Amazon.RegionEndpoint.APSoutheast1))
            {
                var request = new PutObjectRequest()
                {
                    BucketName = _bucketName,
                    CannedACL = S3CannedACL.PublicRead,//PERMISSION TO FILE PUBLIC ACCESIBLE
                    Key = string.Format(path),
                    InputStream = inputSteram,//SEND THE FILE STREAM,                            
                };


                if (file.FileName.Split('.')[1] == "gif")
                {
                    request.ContentType = "image/gif";
                }

                client.PutObject(request);
            }
        }
        catch (Exception ex)
        {

            Logger.Error("Exception Occured while uploading to Amazon S3 : " + ex, ex);
        }

        imgurImage.data = new imgurData();

        imgurImage.data.link = _amazonS3PublicUrl + "/" + _bucketName + "/" + path;
        imgurImage.data.link_s = _amazonS3PublicUrl + "/" + _bucketName + "/" + path;
        imgurImage.data.link_m = _amazonS3PublicUrl + "/" + _bucketName + "/" + path;
        imgurImage.data.link_l = _amazonS3PublicUrl + "/" + _bucketName + "/" + path;
        imgurImage.data.copyText = "";


    return imgurImage;
}

public static byte[] StreamToByteArray(Stream input)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

public static Stream GifImageFileWithoutCompression(Stream imageFileStream) // Set targetSize to 1024
{
    byte[] imageFile = StreamToByteArray(imageFileStream);
    using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
    {
        using (Bitmap newImage = new Bitmap(oldImage.Width, oldImage.Height, PixelFormat.Format24bppRgb))
        {
            MemoryStream m = new MemoryStream();
            newImage.Save(m, ImageFormat.Gif);
            return new MemoryStream(m.GetBuffer());

        }
    }
}

public static Stream ResizeImageFile(Stream imageFileStream, int targetSize) // Set targetSize to 1024
{
    byte[] imageFile = StreamToByteArray(imageFileStream);
    using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
    {
        Size newSize = CalculateDimensions(oldImage.Size, targetSize);
        using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
        {
            using (Graphics canvas = Graphics.FromImage(newImage))
            {
                canvas.SmoothingMode = SmoothingMode.AntiAlias;
                canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
                MemoryStream m = new MemoryStream();
                newImage.Save(m, ImageFormat.Jpeg);
                return new MemoryStream(m.GetBuffer());
            }
        }
    }
}

public static Size CalculateDimensions(Size oldSize, int targetSize)
{
    Size newSize = new Size();
    if (oldSize.Height > oldSize.Width)
    {
        newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height));
        newSize.Height = targetSize;
    }
    else
    {
        newSize.Width = targetSize;
        newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));
    }
    return newSize;
}

public static ImgurImageResponse UploadSingleImageToS3FromPath(String imagePath, string albumid,String fileName)
{


    //var fileName = values[1].Replace(" ", "_") + "_image.png";
    var path = albumid + "/" + fileName;
    var imgurImage = new ImgurImageResponse();

    try
    {
        //path = path + "." + imageType;

        byte[] photo = File.ReadAllBytes(imagePath);
        IAmazonS3 client;                
        using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(_awsAccessKey, _awsSecretKey, Amazon.RegionEndpoint.APSoutheast1))
        {
            var request = new PutObjectRequest()
            {
                BucketName = _bucketName,
                CannedACL = S3CannedACL.PublicRead,//PERMISSION TO FILE PUBLIC ACCESIBLE
                Key = string.Format(path),
                InputStream = new MemoryStream(photo)//SEND THE FILE STREAM
            };

            client.PutObject(request);
            if (File.Exists(@imagePath))
            {
                File.Delete(@imagePath);
            }
        }
    }
    catch (Exception ex)
    {

        Logger.Error("Exception Occured while uploading to Amazon S3 : " + ex, ex);
    }

    imgurImage.data = new imgurData();

    imgurImage.data.link = _amazonS3PublicUrl + "/" + _bucketName + "/" + path;
    imgurImage.data.link_s = _amazonS3PublicUrl + "/" + _bucketName + "/" + path;
    imgurImage.data.link_m = _amazonS3PublicUrl + "/" + _bucketName + "/" + path;
    imgurImage.data.link_l = _amazonS3PublicUrl + "/" + _bucketName + "/" + path;
    imgurImage.data.copyText = "";


    return imgurImage;
}
public static String SaveImageOnServer(String imageUrl, String saveLocation)
{
    byte[] imageBytes;
    HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
    WebResponse imageResponse = imageRequest.GetResponse();

    Stream responseStream = imageResponse.GetResponseStream();

    using (BinaryReader br = new BinaryReader(responseStream))
    {
        imageBytes = br.ReadBytes(500000);
        br.Close();
    }
    responseStream.Close();
    imageResponse.Close();

    FileStream fs = new FileStream(saveLocation, FileMode.Create);
    BinaryWriter bw = new BinaryWriter(fs);
    try
    {
        bw.Write(imageBytes);
    }
    finally
    {
        fs.Close();
        bw.Close();
    }

    return saveLocation;
}
}
}

希望这有助于你。