我有一个公开的测试桶,我使用一个简单的.ps1文件从中下载文件。
我正在尝试相反的操作,上传一个txt文件,但无法正常工作。我试图在不安装gcloud cmdlet b / c的情况下执行此操作,我希望它可以在任何计算机上运行。
$destination = "https://storage.googleapis.com/somerandombucket/test.txt"
$source = "test.txt"
$wc = New-Object System.Net.WebClient
$wc.UploadFile($source, $destination)
这可能吗?我对传递凭据持开放态度,但是我希望不必下载gcloud cmdlet,因为我不是通过URL下载它们的。任何帮助,将不胜感激。
这是Powershell故障通知:
使用“ 2”个参数调用“ UploadFile”的异常:“一个异常 WebClient请求期间发生。”在C:\ xxxxxxxxx \ testupload.ps1:6 字符数:1 + $ wc.UploadFile($ source,$ destination) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:未指定:(:) [],MethodInvocationException + FullyQualifiedErrorId:WebException
答案 0 :(得分:0)
以下是从公共Google Cloud Storage存储桶中下载和上传对象的步骤:
NOTE: This will make the bucket public and anyone from the internet will be able to upload objects and download them.
upload.ps1
PowerShell文件上传对象#Upload file to Google Cloud Storage
$dir = "C:\PATH\TO\OBJECT\image.jpg"
$bucket_name = "[BUCKET_NAME]"
$Uri = "https://www.googleapis.com/upload/storage/v1/b/$bucket_name/o?uploadType=media&name=image.jpg"
Invoke-RestMethod -Method Post -Uri $Uri -Header $header -ContentType "image/jpg" -InFile "$dir"
download.ps1
PowerShell文件下载对象#Download from Google Cloud Storage
$bucket_name = "[BUCKET_NAME]"
$bucket_file = "https://storage.googleapis.com/$bucket_name/image.jpg"
Invoke-RestMethod -Uri "$bucket_file" -Method Get -OutFile C:image.jpg
我已经测试了上面的方法,并且可以使用上述PowerShell脚本向您确认我能够成功下载和上传对象。