Forge chunk 上传 .NET Core
Forge chunk upload .NET Core
我对在 Forge 存储桶中上传大型对象有疑问。我知道我需要使用 /resumable api,但是我怎样才能得到文件(当我只有文件名时)。在这段代码中 FILE_PATH 到底是什么?一般是先存到服务器再上传到bucket吗?
private static dynamic resumableUploadFile()
{
Console.WriteLine("*****begin uploading large file");
string path = FILE_PATH;
if (!File.Exists(path))`enter code here`
path = @"..\..\..\" + FILE_PATH;
//total size of file
long fileSize = new System.IO.FileInfo(path).Length;
//size of piece, say 2M
long chunkSize = 2 * 1024 * 1024 ;
//pieces count
long nbChunks = (long)Math.Round(0.5 + (double)fileSize / (double)chunkSize);
//record a global response for next function.
ApiResponse<dynamic> finalRes = null ;
using (FileStream streamReader = new FileStream(path, FileMode.Open))
{
//unique id of this session
string sessionId = RandomString(12);
for (int i = 0; i < nbChunks; i++)
{
//start binary position of one certain piece
long start = i * chunkSize;
//end binary position of one certain piece
//if the size of last piece is bigger than total size of the file, end binary
// position will be the end binary position of the file
long end = Math.Min(fileSize, (i + 1) * chunkSize) - 1;
//tell Forge about the info of this piece
string range = "bytes " + start + "-" + end + "/" + fileSize;
// length of this piece
long length = end - start + 1;
//read the file stream of this piece
byte[] buffer = new byte[length];
MemoryStream memoryStream = new MemoryStream(buffer);
int nb = streamReader.Read(buffer, 0, (int)length);
memoryStream.Write(buffer, 0, nb);
memoryStream.Position = 0;
//upload the piece to Forge bucket
ApiResponse<dynamic> response = objectsApi.UploadChunkWithHttpInfo(BUCKET_KEY,
FILE_NAME, (int)length, range, sessionId, memoryStream,
"application/octet-stream");
finalRes = response;
if (response.StatusCode == 202){
Console.WriteLine("one certain piece has been uploaded");
continue;
}
else if(response.StatusCode == 200){
Console.WriteLine("the last piece has been uploaded");
}
else{
//any error
Console.WriteLine(response.StatusCode);
break;
}
}
}
return (finalRes);
}
- FILE_PATH: 是您在服务器上存储文件的路径。
- 您应该先将文件上传到服务器。为什么?因为当您将文件上传到 Autodesk Forge Server 时,您需要内部令牌,它应该保密(这就是为什么您将它保存在您的服务器中),您不希望有人拿走该令牌并弄乱您的 Forge 帐户。
您从 this article 粘贴的代码更多是关于 在文件已存储在服务器上时从服务器上传 - 要么用于缓存目的,要么服务器 using/modifying那些文件。
如 Paxton.Huynh 所述,FILE_PATH
包含服务器上存储文件的位置。
如果你只是想通过你的服务器将块上传到 Forge (以保持凭据和内部访问令牌的秘密),就像代理一样,那么最好只是通过在这些块上进行 Forge 而不是将文件存储在服务器上 first 和 then 将其传递给 Forge - 您提到的示例代码在做什么。
参见例如这个,虽然它在 NodeJS: https://github.com/Autodesk-Forge/forge-buckets-tools/blob/master/server/data.management.js#L171
我对在 Forge 存储桶中上传大型对象有疑问。我知道我需要使用 /resumable api,但是我怎样才能得到文件(当我只有文件名时)。在这段代码中 FILE_PATH 到底是什么?一般是先存到服务器再上传到bucket吗?
private static dynamic resumableUploadFile()
{
Console.WriteLine("*****begin uploading large file");
string path = FILE_PATH;
if (!File.Exists(path))`enter code here`
path = @"..\..\..\" + FILE_PATH;
//total size of file
long fileSize = new System.IO.FileInfo(path).Length;
//size of piece, say 2M
long chunkSize = 2 * 1024 * 1024 ;
//pieces count
long nbChunks = (long)Math.Round(0.5 + (double)fileSize / (double)chunkSize);
//record a global response for next function.
ApiResponse<dynamic> finalRes = null ;
using (FileStream streamReader = new FileStream(path, FileMode.Open))
{
//unique id of this session
string sessionId = RandomString(12);
for (int i = 0; i < nbChunks; i++)
{
//start binary position of one certain piece
long start = i * chunkSize;
//end binary position of one certain piece
//if the size of last piece is bigger than total size of the file, end binary
// position will be the end binary position of the file
long end = Math.Min(fileSize, (i + 1) * chunkSize) - 1;
//tell Forge about the info of this piece
string range = "bytes " + start + "-" + end + "/" + fileSize;
// length of this piece
long length = end - start + 1;
//read the file stream of this piece
byte[] buffer = new byte[length];
MemoryStream memoryStream = new MemoryStream(buffer);
int nb = streamReader.Read(buffer, 0, (int)length);
memoryStream.Write(buffer, 0, nb);
memoryStream.Position = 0;
//upload the piece to Forge bucket
ApiResponse<dynamic> response = objectsApi.UploadChunkWithHttpInfo(BUCKET_KEY,
FILE_NAME, (int)length, range, sessionId, memoryStream,
"application/octet-stream");
finalRes = response;
if (response.StatusCode == 202){
Console.WriteLine("one certain piece has been uploaded");
continue;
}
else if(response.StatusCode == 200){
Console.WriteLine("the last piece has been uploaded");
}
else{
//any error
Console.WriteLine(response.StatusCode);
break;
}
}
}
return (finalRes);
}
- FILE_PATH: 是您在服务器上存储文件的路径。
- 您应该先将文件上传到服务器。为什么?因为当您将文件上传到 Autodesk Forge Server 时,您需要内部令牌,它应该保密(这就是为什么您将它保存在您的服务器中),您不希望有人拿走该令牌并弄乱您的 Forge 帐户。
您从 this article 粘贴的代码更多是关于 在文件已存储在服务器上时从服务器上传 - 要么用于缓存目的,要么服务器 using/modifying那些文件。
如 Paxton.Huynh 所述,FILE_PATH
包含服务器上存储文件的位置。
如果你只是想通过你的服务器将块上传到 Forge (以保持凭据和内部访问令牌的秘密),就像代理一样,那么最好只是通过在这些块上进行 Forge 而不是将文件存储在服务器上 first 和 then 将其传递给 Forge - 您提到的示例代码在做什么。
参见例如这个,虽然它在 NodeJS: https://github.com/Autodesk-Forge/forge-buckets-tools/blob/master/server/data.management.js#L171