0

我正在尝试从存储桶中删除一个对象。阅读文档听起来非常简单,但我似乎无法让它工作。在服务器端,我在 ASP.NET 中有以下内容:

[HttpDelete]
[Route("api/forge/oss/objects/delete")]
public async Task<dynamic> DeleteObject_fromBucket()
{
    // basic input validation
    HttpRequest req = HttpContext.Current.Request;

    if (string.IsNullOrWhiteSpace(req.Params["bucketKey"]))
        throw new System.Exception("BucketKey parameter was not provided.");

    if (string.IsNullOrWhiteSpace(req.Params["objectName"]))
        throw new System.Exception("ObjectName parameter was not provided."); 

    string bucketKey = req.Params["bucketKey"];
    string objectName = req.Params["objectName"];

    // call API to delete object on the bucket
    dynamic oauth = await OAuthController.GetInternalAsync();
    ObjectsApi objects = new ObjectsApi();
    string access_token = oauth.access_token; ;
    objects.Configuration.AccessToken = access_token;

    // delete the file/object
    await objects.DeleteObjectAsync(bucketKey, objectName);
    return 0;
}

客户端:

function deleteObject(node) {
    result = window.confirm('Wollen Sie dieses Modell löschen');
    if (result == false) { return; }
    else {
        var bucketKey = node.parents[0];
        var objectName = node.text;

        var formData = new FormData();
        formData.append('bucketKey', bucketKey);
        formData.append('objectName', objectName);

        $.ajax({
            url: 'api/forge/oss/objects/delete',
            data: formData,
            contentType: false,  
            processData: false,
            type: 'DELETE',  // man könnte auch method: schreiben
            success: function (data) {
                $('#appBuckets').jstree(true).refresh_node(node);
            }
        });
    }       
}

我总是遇到无法进行 API 调用的异常。bucketKey 和 objectName 都是字符串。谁能帮我理解我哪里出错了?

非常感谢。

4

2 回答 2

0

这周我碰巧为悉尼加速器的一位与会者编写了一个工作代码。该代码片段在Learn Forge Tutorial (2 legged workflow)上进行了测试。一个是删除bucket,一个是删除object。看起来您也在使用该教程框架进行测试?

一开始我做了一个和你类似的代码,但是我的VS在编译时抛出了错误。最后,我发现这是由于返回值。由于它是一个 HTTP 请求,因此看起来 0 对响应没有意义。另外,该教程中内部令牌的默认范围不包含桶删除和数据写入(用于删除对象)。我得到了客户端响应的详细错误。

在 OAuthController.cs 添加这些范围之后。一切开始工作:

 public static async Task<dynamic> GetInternalAsync()
 {
  if (InternalToken == null || InternalToken.ExpiresAt < 
   DateTime.UtcNow)
  {
    InternalToken = await Get2LeggedTokenAsync(new Scope[] { 
       Scope.BucketCreate, Scope.BucketRead, Scope.DataRead, 
       Scope.DataCreate,Scope.BucketDelete,Scope.DataWrite});
        InternalToken.ExpiresAt = 
    DateTime.UtcNow.AddSeconds(InternalToken.expires_in);
  }

  return InternalToken;
}

如果这些对您的情况没有帮助,我建议构建Forge SDK 源项目,添加到您的项目中,并调试相应的方法以查看确切的错误是什么。如果您对此有任何疑问,请随时告诉我们。

服务器端:

    [HttpPost]
    [Route("api/forge/oss/buckets/delete")]
    public async Task<dynamic> DeleteBucket([FromBody]CreateBucketModel bucket)
    {
        BucketsApi buckets = new BucketsApi();
        dynamic token = await OAuthController.GetInternalAsync();
        buckets.Configuration.AccessToken = token.access_token; 

        await buckets.DeleteBucketAsync(bucket.bucketKey); 
        //or
        //buckets.DeleteBucket(bucket.bucketKey); 

        return Task.CompletedTask;
    }

    [HttpPost]
    [Route("api/forge/oss/objects/delete")]
    public async Task<dynamic> DeleteObject([FromBody]DeleteObjectModel 
                                                      objInfo)
    {
        ObjectsApi objs = new ObjectsApi();
        dynamic token = await OAuthController.GetInternalAsync();
        objs.Configuration.AccessToken = token.access_token;

        await objs.DeleteObjectAsync(objInfo.bucketKey, objInfo.objectKey);

        //or
        //objs.DeleteObject(objInfo.bucketKey, objInfo.objectKey); 

        return Task.CompletedTask;
    }

    public class CreateBucketModel
    {
      public string bucketKey { get; set; }
    }

    public class DeleteObjectModel
    {
       public string bucketKey { get; set; }
       public string objectKey { get; set; } 
     }

客户端:

function deleteBucket() {
     //select one bucket node of the tree
     var bucketKey = $('#appBuckets').jstree(true).get_selected(true)[0].id;

     var policyKey = $('#newBucketPolicyKey').val();
     jQuery.post({
        url: '/api/forge/oss/buckets/delete',
             contentType: 'application/json',
             data: JSON.stringify({ 'bucketKey': bucketKey, 
                                    'policyKey': policyKey }),
             success: function (res) {
               $('#appBuckets').jstree(true).refresh();
               alert('Bucket  deleted') 
              },
             error: function (err) {
               alert('Bucket not deleted')
               console.log(err);
             }
         });
     }

   function deleteObject() {
      //assume the first selected tree node is bucket
      var bucketKey = $('#appBuckets').jstree(true).get_selected(true)[0].text;
      //assume the second selected tree node is object
      var objectKey = $('#appBuckets').jstree(true).get_selected(true)[1].text;
      jQuery.post({
          url: '/api/forge/oss/objects/delete',
          contentType: 'application/json',
          data: JSON.stringify({ 'bucketKey': bucketKey, 
                                 'objectKey': objectKey }),
          success: function (res) {
             $('#appBuckets').jstree(true).refresh();
             alert('Object  deleted') 
          },
          error: function (err) {
            alert('Object not deleted')
            console.log(err);
          }
      });
 }
于 2018-07-14T06:31:35.473 回答
0

URL 应该是 url: '/api/forge/oss/objects/delete' 而不是 url: 'api/forge/oss/objects/delete'

于 2018-07-13T06:01:47.590 回答