我正在尝试从存储桶中删除对象。阅读文档听起来很简单,但我似乎无法使其正常运行。在服务器端,我在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都是字符串。谁能帮助我了解我要去哪里哪里?
非常感谢。
答案 0 :(得分:0)
URL应该是 网址:“ / api / forge / oss / objects / delete” 而不是网址:“ api / forge / oss / objects / delete”
答案 1 :(得分:0)
本周,我碰巧为Sydney Accelerator中的一位与会者编写了工作代码。该代码段已在Learn Forge Tutorial (2 legged workflow)上进行了测试。一种是删除存储桶,另一种是删除对象。看来您也在使用该教程的框架进行测试?
一开始我做了类似您的代码,但是我的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 source project,添加到您的项目中,并调试相应的方法以查看确切的错误是什么。如果您对此有任何疑问,请随时告诉我们。
服务器端:
[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);
}
});
}