尽管我正确地输入了CreateBucket,但我还是不断地出现错误



我正在努力学习使用forgeapi。我能够使用OAuthController获取令牌。然后我想创建一个bucket。我正试图使用OSSController";CreateBucket";函数,但即使我在给出";bucketkey";数据我举的例子是";testforge"test123";等等。我输入的数据是正确的,但我一直收到这个错误。

错误:";Autodesk。锻造客户ApiException:调用CreateBucket时出错:{原因:有效字段"bucketKey"的格式必须为[-_.a-z0-9]{3128}}&"。

> 
using Autodesk.Forge;
using Autodesk.Forge.Model;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace ForgeDeneme.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class OSSController : ControllerBase
{
private IWebHostEnvironment _env;
public OSSController(IWebHostEnvironment env) { _env = env; }
public string ClientId = "MyClientId";
/// <summary>
/// Return list of buckets (id=#) or list of objects (id=bucketKey)
/// </summary>
[HttpGet]
[Route("api/forge/oss/buckets")]
public async Task<IList<TreeNode>> GetOSSAsync(string id)
{
IList<TreeNode> nodes = new List<TreeNode>();
dynamic oauth = await OAuthController.GetInternalAsync();
if (id == "#") // root
{
// in this case, let's return all buckets
BucketsApi appBckets = new BucketsApi();
appBckets.Configuration.AccessToken = oauth.access_token;
// to simplify, let's return only the first 100 buckets
dynamic buckets = await appBckets.GetBucketsAsync("US", 100);
foreach (KeyValuePair<string, dynamic> bucket in new DynamicDictionaryItems(buckets.items))
{
nodes.Add(new TreeNode(bucket.Value.bucketKey, bucket.Value.bucketKey.Replace(ClientId + "-", string.Empty), "bucket", true));
}
}
else
{
// as we have the id (bucketKey), let's return all 
ObjectsApi objects = new ObjectsApi();
objects.Configuration.AccessToken = oauth.access_token;
var objectsList = await objects.GetObjectsAsync(id, 100);
foreach (KeyValuePair<string, dynamic> objInfo in new DynamicDictionaryItems(objectsList.items))
{
nodes.Add(new TreeNode(Base64Encode((string)objInfo.Value.objectId),
objInfo.Value.objectKey, "object", false));
}
}
return nodes;
}
/// <summary>
/// Model data for jsTree used on GetOSSAsync
/// </summary>
public class TreeNode
{
public TreeNode(string id, string text, string type, bool children)
{
this.id = id;
this.text = text;
this.type = type;
this.children = children;
}
public string id { get; set; }
public string text { get; set; }
public string type { get; set; }
public bool children { get; set; }
}
/// <summary>
/// Create a new bucket 
/// </summary>
[HttpPost]
[Route("api/forge/oss/buckets")]
public async Task<dynamic> CreateBucket([FromBody] CreateBucketModel bucket)
{
BucketsApi buckets = new BucketsApi();
dynamic token = await OAuthController.GetInternalAsync();
buckets.Configuration.AccessToken = token.access_token;
PostBucketsPayload bucketPayload = new PostBucketsPayload(string.Format("{0}-{1}", ClientId, bucket.bucketKey), null,
PostBucketsPayload.PolicyKeyEnum.Transient);
return await buckets.CreateBucketAsync(bucketPayload, "US");
}

/// <summary>
/// Input model for CreateBucket method
/// </summary>
public class CreateBucketModel
{
public string bucketKey { get; set; }
}
/// <summary>
/// Receive a file from the client and upload to the bucket
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("api/forge/oss/objects")]
public async Task<dynamic> UploadObject([FromForm] UploadFile input)
{
// save the file on the server
var fileSavePath = Path.Combine(_env.WebRootPath, Path.GetFileName(input.fileToUpload.FileName));
using (var stream = new FileStream(fileSavePath, FileMode.Create))
await input.fileToUpload.CopyToAsync(stream);

// get the bucket...
dynamic oauth = await OAuthController.GetInternalAsync();
ObjectsApi objects = new ObjectsApi();
objects.Configuration.AccessToken = oauth.access_token;
// upload the file/object, which will create a new object
dynamic uploadedObj;
using (StreamReader streamReader = new StreamReader(fileSavePath))
{
uploadedObj = await objects.UploadObjectAsync(input.bucketKey,
Path.GetFileName(input.fileToUpload.FileName), (int)streamReader.BaseStream.Length, streamReader.BaseStream,
"application/octet-stream");
}
// cleanup
System.IO.File.Delete(fileSavePath);
return uploadedObj;
}
public class UploadFile
{
public string bucketKey { get; set; }
public IFormFile fileToUpload { get; set; }
}
/// <summary>
/// Base64 enconde a string
/// </summary>
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
}
}

出错后,我想和邮递员一起做事。当他向我索要代币时,我想用我使用的代币进行交易。Net Core,即使用forgeapi包,但该令牌未被接受。然后,我使用我通过poster查询获得的令牌,并将post操作作为bucketKey:";test1";。结果,我的交易发生了。你的Forge-api-nuget包中有错误吗?还是我的代码中有错误?

当bucketKey提交给服务时,请确保您遵循了此处的示例,并将ClientID转换为小写:

public string ClientId { get { return OAuthController.GetAppSetting("FORGE_CLIENT_ID").ToLower(); } }

如果问题仍然存在,请完整发布您的代码,以便我们可以调查该问题。

相关内容

最新更新