【问题标题】:Issues uploading a video file into Amazon S3 using ASP.NET C#使用 ASP.NET C# 将视频文件上传到 Amazon S3 的问题
【发布时间】:2011-05-20 06:14:49
【问题描述】:

我开发了一个 ASP.NET (C#) 应用程序来将图像和视频存储到 Amazon S3 中。图像上传正常,但当我尝试上传视频时,它会在 Amazon S3 中保存为图像格式。

有谁知道问题出在哪里或如何上传视频?

private void Amzon(string imageName,string imgcontenttype,int imglength,byte[] fileData)
{
    AmazonS3 myS3 = new AmazonS3();
    DateTime myTime = DateTime.Now;

    // Create a signature for this operation
    string strMySignature = S3Helper.GetSignature(mySecretAccessKeyId, "PutObjectInline", myTime);

    // Create a new Access grant for anonymous users.
    Grant myGrant = new Grant();
    Grant[] myGrants = new Grant[1];

    // Setup Access control, allow Read access to all
    Group myGroup = new Group();
    myGroup.URI = "http://acs.amazonaws.com/groups/global/AllUsers";
    myGrant.Grantee = myGroup;
    myGrant.Permission = Permission.READ;
    myGrants[0] = myGrant;
    string key = imageName;
    // Setup some metadata to indicate the content type
    MetadataEntry myContentType = new MetadataEntry();
    myContentType.Name = "ContentType";
    myContentType.Value = imgcontenttype;

    MetadataEntry[] myMetaData = new MetadataEntry[1];
    myMetaData[0] = myContentType;

    // Finally upload the object
    PutObjectResult myResult = myS3.PutObjectInline(
        bucketname,
        key,
        myMetaData,
        fileData,
        imglength,
        myGrants,
        StorageClass.STANDARD,
        true,
        myAWSAccessKeyId,
        S3Helper.GetTimeStamp(myTime),
        true,
        strMySignature, null
        );

    // Print out the results.
    if (myResult != null)
    {
        cn.Open();
        Url = "https://s3.amazonaws.com/" + bucketname + "/" + key;
        string Query = "Insert into S3Image(ImageName,ImageUrl)Values('" + key + "','" + Url + "')";
        SqlCommand cmd = new SqlCommand(Query, cn);
        cmd.ExecuteNonQuery();
        cn.Close();
        //MyPrint("ETag: " + myResult.ETag);
        MyPrint("<img src=https://s3.amazonaws.com/" + bucketname + "/" + key);
    }
}

谢谢。

【问题讨论】:

  • 您要上传什么格式的视频?视频的扩展名是什么?你能给我们看一些代码吗?
  • 感谢您的回复,我已尝试上传 .mp4 格式文件
  • 你能给我们看一些代码吗?如果没有它,很难弄清楚问题可能是什么。
  • 嗨,我已将我的代码粘贴到我上面的问题中,请找到该代码..
  • 您好,对此问题有任何想法,请告诉我......

标签: c# asp.net amazon-s3


【解决方案1】:

有很多代码可以启动和运行亚马逊的网络服务。我认为这部分代码是您需要的,您可能没有设置正确的内容类型:

// Setup some metadata to indicate the content type 
        MetadataEntry myContentType = new MetadataEntry();
        myContentType.Name = "ContentType";
        myContentType.Value = FileUpload1.PostedFile.ContentType;

这是完整的代码:享受。

`private const string accessKeyId = "REMOVED";
    private const string secretAccessKey = "REMOVED";
    private static DateTime GetTimeStamp(DateTime myTime)
    {
        DateTime myUniversalTime = myTime.ToUniversalTime();
        DateTime myNewTime = new DateTime(myUniversalTime.Year,
        myUniversalTime.Month, myUniversalTime.Day,
        myUniversalTime.Hour, myUniversalTime.Minute,
        myUniversalTime.Second, myUniversalTime.Millisecond);

        return myNewTime;
    }
    private static string GetSignature(string secretAccessKey, string strOperation, DateTime myTime)
    {
        Encoding myEncoding = new UTF8Encoding();

        // Create the source string which is used to create the digest
        string mySource = "AmazonS3" + strOperation + FormatTimeStamp(myTime);

        // Create a new Cryptography class using the 
        // Secret Access Key as the key
        HMACSHA1 myCrypto = new HMACSHA1(myEncoding.GetBytes(secretAccessKey));

        // Convert the source string to an array of bytes
        char[] mySourceArray = mySource.ToCharArray();
        // Convert the source to a UTF8 encoded array of bytes
        byte[] myUTF8Bytes = myEncoding.GetBytes(mySourceArray);
        // Calculate the digest 
        byte[] strDigest = myCrypto.ComputeHash(myUTF8Bytes);
        return Convert.ToBase64String(strDigest);
    }
    private static string FormatTimeStamp(DateTime myTime)
    {
        DateTime myUniversalTime = myTime.ToUniversalTime();
        return myUniversalTime.ToString("yyyy-MM-dd\\THH:mm:ss.fff\\Z", System.Globalization.CultureInfo.InvariantCulture);
    }
    /// <summary>
    /// Upload Images.
    /// </summary>
    /// <param name="a">Ex. FileUpload1.PostedFile.ContentType</param>
    /// <param name="b">Ex. FileUpload1.PostedFile.FileName</param>
    /// <param name="c">Ex. FileUpload1.FileBytes</param>
    /// <param name="d">Ex. FileUpload1.FileBytes.Length</param>
    /// <param name="id">The ID for this Product Group</param>
    public void UploadImage_ProductGroup(string a, string b, byte[] c, long d, int id)
    {
        AmazonS3 myS3 = new AmazonS3();
        DateTime myTime = DateTime.Now;

        // Create a signature for this operation
        string strMySignature = GetSignature(
        secretAccessKey,
        "PutObjectInline",
        myTime);

        // Create a new Access grant for anonymous users.
        Grant myGrant = new Grant();
        Grant[] myGrants = new Grant[1];

        // Setup Access control, allow Read access to all
        Group myGroup = new Group();
        myGroup.URI = "http://acs.amazonaws.com/groups/global/AllUsers";
        myGrant.Grantee = myGroup;
        myGrant.Permission = Permission.READ;
        myGrants[0] = myGrant;

        // Setup some metadata to indicate the content type 
        MetadataEntry myContentType = new MetadataEntry();
        myContentType.Name = "ContentType";
        myContentType.Value = a;

        MetadataEntry[] myMetaData = new MetadataEntry[1];
        myMetaData[0] = myContentType;

        //Format the file name to prepend thumbnail before the file extension.
      /*  int lastIndex = b.LastIndexOf('.');
        string fileName = b.Remove(lastIndex);
        string ext = b.Remove(0, lastIndex);
        string thumbPath = string.Format("images/public/{0}thumb{1}",fileName,ext);
        //Resize the thumbnail
        */



        // Finally upload the object
        PutObjectResult myResult = myS3.PutObjectInline(
            "mywebsite",
        "images/public/" + b,
            myMetaData,
            c,
            d,
            myGrants,
            StorageClass.STANDARD,
            true,
            accessKeyId,
            GetTimeStamp(myTime),
            true,
            strMySignature, null
        );`

【讨论】:

  • 嗨尼克谢谢你的回应,你能发布整个上传课程。然后只有我能找到我的问题..上面我已经发布了我的代码,我为上传做了什么,请看那个代码......
  • 您有一个名为 imgcontenttype 的变量,我无法判断您实际传递给它的内容类型。
  • 我感觉可能是这种情况。
  • 只是我将内容类型分配给我从上传文件中获得的变量..但是你像 FileUpload1.PostedFile.ContentType 一样直接分配;
  • 嗨,它工作正常谢谢,但它只接受 1mb 文件。它不接受更多,我如何上传超过 1mb 的文件,请帮助我...
猜你喜欢
  • 2013-09-09
  • 2013-05-19
  • 2014-10-05
  • 2017-08-05
  • 2014-08-06
  • 2019-03-03
  • 2013-10-26
  • 2011-06-09
  • 2019-10-26
相关资源
最近更新 更多