使用Powershell将文件上传到Box.com [英] Upload a file to Box.com using Powershell
问题描述
我正在为公司使用一系列Powershell脚本,可用于在Box.com之间传输数据。我无法确定的一件事是上传文件。 Box API需要进行上载的多步POST操作,我在SO上看到了一些答案,表明我应该能够在Powershell中执行此操作(例如这一个)。但是我似乎无法正常工作。
I am working on a series of Powershell scripts for my company to use to transfer data to and from Box.com. The one thing I just can't figure out is uploading files. The Box API requires a multipart POST operation for uploads, and I've seen a few answers here on SO indicating that I should be able to do that in Powershell (such as this one). But I can't seem to get it working.
这是我现在拥有的代码:
Here's the code I have right now:
Function Post-File {
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$SourcePath,
[Parameter(Mandatory=$False,Position=2)]
[string]$FolderId = ############
)
#Variables for building URIs
$baseUrl = "https://upload.box.com/api/2.0/files/content"
#Set Authorization for API requests
$headers = @{}
$AccessToken = Refresh-Tokens #A reference to another function that definitely works
$headers.Add("Authorization", "Bearer $AccessToken")
#Set POST content
$body = @{}
$body.Add("filename", [IO.File]::ReadAllBytes($SourcePath))
$body.Add("parent_id", $FolderId)
#Upload the file
Invoke-RestMethod -Uri $baseUrl -Method Post -Headers $headers -ContentType "multipart/form-data" -Body $body
}
这是我的回复回来:
{
"type":"error",
"status":400,
"code":"invalid_request_parameters",
"help_url":"http://developers.box.com/docs/#errors",
"message":"Invalid input parameters in request",
"request_id":"1764475572534bcddfe04b7"
}
I'我们还尝试了其他几种无效的排列方式。我尝试在 Invoke-RestMethod
中使用 -InFile
开关,而不是 -Body
。我还尝试使用 Get-Content -Raw
代替 [IO.File] :: ReadAllBytes
。两者都返回一个更通用的错误:服务器遇到内部错误或配置错误,无法完成您的请求。
。
I've also tried a couple of other permutations that aren't working. I've tried using the -InFile
switch in Invoke-RestMethod
instead of -Body
. I've also tried using Get-Content -Raw
in place of [IO.File]::ReadAllBytes
. Both of those return a more generic error: The server encountered an internal error or misconfiguration and was unable to complete your request.
.
我很确定这与我的 filename
参数不正确有关,但是我不知道如何解决它。根据 Box API ,这是它的外观卷曲。有人可以帮我为Powershell正确翻译吗?
I'm pretty sure this has something to do with my filename
parameter not being correct, but I'm not sure how to fix it. According to the Box API, here's what it should look like in curl. Can someone help me properly translate this for Powershell?
https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer ACCESS_TOKEN" \
-F filename=@FILE_NAME \
-F parent_id=PARENT_FOLDER_ID
推荐答案
在PowerShell中有一些使这有些棘手的事情:
There are a couple things that make this a little tricky in PowerShell:
-
文件名
参数指定文件名,而不是内容。 - 需要一个请求主体来指定文件名和目标。
- PowerShell对待
-InFile
和-Body
参数是互斥的。 - PowerShell似乎并不原生支持
multipart / form-data
根据您问题的帖子
- The
filename
parameter specifies the name of the file, not the contents. - A request body is required in order to specify the file name and destination.
- PowerShell treats
-InFile
and-Body
arguments as mutually exclusive. - PowerShell does not appear to natively support
multipart/form-data
POSTs, per the question that you referenced.
请求正文是必需的(1,2)-因此不能使用 -InFile
(3)-我认为您可能需要滚动自己的 multipart / form-data
正文(4),其中包含必要的元数据和文件内容。 此博客文章描述了一种这样做的方法。内容是短字符串(一条推文),但原理是相同的。
Since the request body is required (1,2) -- and thus -InFile
can't be used (3) -- I think you might need to roll your own multipart/form-data
body (4) that contains the necessary metadata and file content. This blog post describes a method for doing so. The content there is a short string (a tweet) but the principle is the same.
下面是我刚刚使用Box Windows SDK执行的上载的Fiddler跟踪。这显示了请求通过网络时的外观。 $ BOUNDARY
是任意的,唯一的字符串-GUID很好用。
Below is a Fiddler trace of an upload I just performed using the Box Windows SDK. This shows how the request should look as it goes across the wire. The $BOUNDARY
is an arbitrary, unique string -- a GUID works great.
POST https://upload.box.com/api/2.0/files/content HTTP/1.1
Authorization: Bearer $TOKEN
Content-Type: multipart/form-data; boundary="$BOUNDARY"
Host: upload.box.com
Content-Length: 2118
Accept-Encoding: gzip, deflate
--$BOUNDARY
Content-Disposition: form-data; name="file"; filename="$FILENAME"
<THE CONTENT OF $FILENAME>
--$BOUNDARY
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="metadata"
{"parent":{"id":"$PARENT_FOLDER_ID"},"name":"$FILENAME"}
--$BOUNDARY--
这是我收到的回复:
HTTP/1.1 201 Created
Date: Mon, 14 Apr 2014 12:52:33 GMT
Server: Apache
Cache-Control: no-cache, no-store
Content-Length: 364
Connection: close
Content-Type: application/json
{"total_count":1,"entries":[{"type":"file","id":"$ID","name":"$FILENAME", ... }]}
这篇关于使用Powershell将文件上传到Box.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!