aws s3 使用webapi上傳文件


前言:最近使用紫光雲的OSS存儲圖片,他們使用aws s3存儲的圖片,給我的文檔很難讀懂.網上翻閱資料,實現webapi上傳文件

1.下載Amazon提供的sdk

2.實現代碼

using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Web;
namespace OSS{
     

 public class OSSUploadTest
 {
/// <summary>
/// 桶名稱
/// </summary>
private const string bucketName = "****";
private static string accessKey = System.Configuration.ConfigurationManager.AppSettings["AWSAccessKey"];
private static string secretKey = System.Configuration.ConfigurationManager.AppSettings["AWSSecretKey"];
/// <summary>
        /// 文件上傳
        /// </summary>
        /// <param name="file">文件</param>
        /// <param name="folder">文件夾</param>
        /// <returns></returns>
public bool UploadFile(HttpPostedFileBase file, string folder)
        {
            try
            {
                var config = new AmazonS3Config()
                {
                    ServiceURL = "http://***.unicloudsrv.com/"
                };
                using (AmazonS3Client client = new AmazonS3Client(
                    accessKey,
                    secretKey,
                    config
                ))
                {
                    var stream = file.InputStream;
                    string name = folder + "/" + file.FileName;
                    var putObjectRequest = new PutObjectRequest
                    {
                        BucketName = bucketName,
                        Timeout = TimeSpan.FromSeconds(5),
                        InputStream = stream,
                        Key = name,
                        CannedACL = S3CannedACL.PublicRead,
                        StorageClass = S3StorageClass.Standard,
                        ServerSideEncryptionMethod = ServerSideEncryptionMethod.None,
                    };
                    var data = client.PutObject(putObjectRequest);
                    if (data.HttpStatusCode == System.Net.HttpStatusCode.OK)
                    {
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                return false;
            }
        }
    }
 }
View Code

其中:

bucketName 是桶名稱

ServiceURL 是Endpoint

accessKey 是  AccessKey ID

secretKey 是  AccessKey Secret

 AccessKey ID和AccessKey Secret是您訪問紫光雲API的密鑰,在控制台查看(配置在web.config中)

3.控制器

/// <summary>

/// 上傳圖片

/// </summary>

/// <returns></returns>

[HttpPost]

[Route("v3/uploads")]

public IHttpActionResult Uploads()

{

    var id = "123";

    HttpPostedFile file = HttpContext.Current.Request.Files["file"];

    if (string.IsNullOrEmpty(id))

    {

        return Json(false);

    }

    string type = "receipt";

    var folder = $"{type}/{id}";

    var data = new OSSUploadTest().UploadFile(new HttpPostedFileWrapper(file) as HttpPostedFileBase, folder);

    return Json(data);
View Code


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM