【问题标题】:Upload thumbnail image to vimeo via api call c#通过api调用c#将缩略图上传到vimeo
【发布时间】:2016-04-02 15:04:36
【问题描述】:

我正在尝试为通过 vimeo api 完成的拉视频上传设置缩略图。我正在为 c# windows 服务开发这个,请注意没有官方库。目前我使用this 库。我可以按照 vimeo 文档成功上传视频,但是,当我尝试将图像上传为视频的缩略图时,我遇到了问题。根据vimeo picutre upload documentation,在第 2 步中,我需要通过 PUT 请求上传我的缩略图。它说,我需要执行以下操作:

PUT https://i.cloud.vimeo.com/video/518016424
.... binary data of your file in the body ....

我不知道该怎么做。我可以使用

获取图像的二进制数据
byte[] byte_array_of_image = File.ReadAllBytes(file);

但是我怎样才能将这些数据发送到 api 并获得响应(使用或不使用库)?如果有帮助,这是我上传到目前为止完成的视频和缩略图的代码。

var vc = VimeoClient.ReAuthorize(
            accessToken: ConfigurationManager.AppSettings["ACCESS_TOKEN"],
            cid: ConfigurationManager.AppSettings["API_KEY"],
            secret: ConfigurationManager.AppSettings["API_SECRET"]
            );
            string temporary_video_dir = ConfigurationManager.AppSettings["TEMP_VIDEO_URL"];
            Dictionary<string,string> automatic_pull_parameters = new Dictionary<string, string>();
            automatic_pull_parameters.Add("type", "pull");
            automatic_pull_parameters.Add("link", temporary_video_dir);
var video_upload_request = vc.Request("/me/videos", automatic_pull_parameters, "POST");
                string uploaded_URI = video_upload_request["uri"].ToString();
                string video_id = uploaded_URI.Split('/')[2];
                Library.WriteErrorLog("Succesfully uploaded Video in test folder. Returned Vimeo ID for video: "+ video_id);
 var picture_resource_request = vc.Request("/videos/" + video_id + "/pictures", null, "POST");
                string picture_resource_link = picture_resource_request["uri"].ToString();
                //Library.WriteErrorLog("uri: " + picture_resource_link);

                byte[] binary_image_data = File.ReadAllBytes("http://testclient.xitech.com.au/Videos/Images/Closing_2051.jpg");
                string thumbnail_upload_link = picture_resource_link.Split('/')[4];

请帮忙!现在卡了几个小时。

【问题讨论】:

    标签: c# image file-upload vimeo vimeo-api


    【解决方案1】:

    参考帖子:-Vimeo API C# - Uploading a video

    答案没有被赞成,但在我的情况下可以尝试。 看下面的代码:-

         public ActionResult UploadChapterVideoVimeo(HttpPostedFileBase file, string productID = "")
    {
        if (file != null){      
                          var authCheck = Task.Run(async () => await vimeoClient.GetAccountInformationAsync()).Result;
                    if (authCheck.Name != null)
                    {
                        
                        BinaryContent binaryContent = new BinaryContent(file.InputStream, file.ContentType);
                        int chunkSize = 0;
                        int contenetLength = file.ContentLength;
                        int temp1 = contenetLength / 1024;
                        if (temp1 > 1)
                        {
                            chunkSize = temp1 / 1024;
                            chunkSize = chunkSize * 1048576;
                        }
                        else
                        { chunkSize = chunkSize * 1048576; }
                        binaryContent.OriginalFileName = file.FileName;
                        var d = Task.Run(async () => await vimeoClient.UploadEntireFileAsync(binaryContent, chunkSize, null)).Result;
                        vmodel.chapter_vimeo_url = "VIMEO-" + d.ClipUri;
                    }
    
                    return RedirectToAction("ProductBuilder", "Products", new { productId = EncryptedProductID, message = "Successfully Uploaded video", type = 1 });
                }
            }
            catch (Exception exc)
            {
                return RedirectToAction("ProductBuilder", "Products", new { productId = EncryptedProductID, message = "Failed to Uploaded video " + exc.Message, type = 0 });
            }
        }
        return null;        }
    

    【讨论】:

      【解决方案2】:

      WebClient 有一个名为UploadData 的方法,它像手套一样合身。下面有一个例子说明你可以做什么。

      WebClient wb = new WebClient();
      wb.Headers.Add("Authorization","Bearer" +AccessToken);
      var file =  wb.DownloadData(new Uri("http://testclient.xitech.com.au/Videos/Images/Closing_2051.jpg"));
      var asByteArrayContent = wb.UploadData(new Uri(picture_resource_request ), "PUT", file);
      var asStringContent = Encoding.UTF8.GetString(asByteArrayContent);
      

      【讨论】:

      • 感谢@Cleiton,但是使用这种方法时如何从服务器获得响应?我需要它来获取图片 ID。
      • 很抱歉,您应该改用UploadData,它会返回一个字节数组,您可以使用Encoding.UTF8.GetString 将其转换为字符串。
      • 从哪里获得returnContent 变量?这不清楚。
      • 是uploaddata的方法,我把这部分改写得更清楚了,忘记改了。对不起。
      • 好的,这很清楚,但现在我得到一个错误:System.ArgumentException: URI formats are not supported. 我会寻找解决办法,如果你知道为什么会这样,请解释一下。
      猜你喜欢
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 2015-07-16
      • 2017-10-24
      • 2014-06-05
      • 1970-01-01
      相关资源
      最近更新 更多