Rest 使用Powershell将文件上载到Box.com
我正在为我的公司编写一系列Powershell脚本,用于在Box.com之间传输数据。我唯一搞不清楚的是上传文件。Box API需要一个用于上传的多部分POST操作,我在这里看到了一些答案,这表明我应该能够在Powershell中做到这一点(例如)。但我似乎无法让它工作 以下是我现在掌握的代码:Rest 使用Powershell将文件上载到Box.com,rest,powershell,box-api,Rest,Powershell,Box Api,我正在为我的公司编写一系列Powershell脚本,用于在Box.com之间传输数据。我唯一搞不清楚的是上传文件。Box API需要一个用于上传的多部分POST操作,我在这里看到了一些答案,这表明我应该能够在Powershell中做到这一点(例如)。但我似乎无法让它工作 以下是我现在掌握的代码: Function Post-File { Param( [Parameter(Mandatory=$True,Position=1)] [stri
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"
}
我还尝试了其他几种不起作用的排列。我已经尝试在调用RestMethod
中使用-infle
开关,而不是-Body
。我还尝试使用获取内容-Raw
代替[IO.File]::ReadAllBytes
。这两个都返回一个更一般的错误:服务器遇到内部错误或配置错误,无法完成您的请求。
我很确定这与我的filename
参数不正确有关,但我不确定如何修复它。根据,这是它卷曲时的样子。有人能帮我把这个翻译成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
我可能听起来很傻,但当你这么做的时候会发生什么
$body.Add("filename", $([IO.File]::ReadAllBytes($SourcePath)))
在PowerShell中,有几件事让这件事变得有点棘手:
filename
参数指定文件名,而不是内容-infle
和-Body
参数视为互斥的multipart/form data
post-infle
(3)——我认为您可能需要滚动自己的多部分/表单数据
主体(4),其中包含必要的元数据和文件内容。描述执行此操作的方法。那里的内容是一个短字符串(tweet),但原理是一样的
下面是我刚刚使用Box Windows SDK执行的上传的Fiddler跟踪。这显示了请求在通过线路时的外观。$BOUNDARY
是一个任意的、唯一的字符串——GUID非常有效
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--
恐怕是同样的回答-(太棒了,谢谢你。我有一种感觉,我可能需要一段时间来实现这一点,但是如果我能让它工作,我一定会开始工作,接受这个答案。热狗!很高兴能帮上忙。”杰弗罗森伯格你会考虑把你自己的卷轴放回原处吗?有很多问题让人苦苦思索。来自Spray 1.1.1服务器的erg建议给出了错误:“具有多部分媒体类型的内容类型必须具有非空的“边界”参数”
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", ... }]}