AppleScript 为 Amazon s3 执行 shell 脚本 cURL 命令 [英] AppleScript do shell script cURL command for Amazon s3

查看:28
本文介绍了AppleScript 为 Amazon s3 执行 shell 脚本 cURL 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我专门在 Mac OSX 环境中工作,我正在尝试将 .zip 文件自动上传到我的 amazon s3 存储桶.

I work exclusively in a Mac OSX environment and I am trying to automate uploading .zip files onto my amazon s3 bucket.

过去几天我一直在尝试,但我真的很困惑.我已经尝试了我将发布的内容的许多不同变体,但我总是得到相同的错误结果.我什至尝试让我的 AppleScript 调用 .sh 文件,但仍然无法得到结果.

I have been trying the last couple of days and I am really stumped. I have tried many different variations of what I will post and I always get the same result of errors. I even tried having my AppleScript call a .sh file and still could not get a result.

我不会只发布我正在执行 cURL 命令的部分代码.我的包含整个 cURL 命令的字符串都在一行中,因为我读过它最适合 AppleScript 理解.

I am not going to post my whole code just the part where I am doing the cURL command. My string that contains my entire cURL command is all in one line because I read that was best for AppleScript to understand.

    set objectName to "My\\ Big\\ Test.zip"
    set dateFormatted to do shell script "date -R"
    set s3Bucket to "zipsOnAmazon"
    set fileName to zipFile
    set relativePath to (" " & s3Bucket & "/" & objectName & " ")
    set s3AccessKey to "my-key"
    set s3SecretKey to "my-secret"
    set contentType to "application/octet-stream"
    set stringToSign to "PUT " & contentType & " " & dateFormatted & relativePath & " "

    set signature to "echo -en " & stringToSign & " | openssl sha1 -hmac " & s3SecretKey & " - binary | base64"

    set theBigCommand to "-X PUT -T " & fileName & " \\ -H \"Host: " & s3Bucket & ".s3-us-west-2.amazonaws.com \\ -H \"Date: " & dateFormatted & " \\ -H \"Content-Type: " & contentType & " \\ -H \"Authorization: AWS " & s3AccessKey & ":" & signature & " \\ https://" & s3Bucket & ".s3-us-west-2.amazonaws.com/" & objectName & ""

    display dialog ("curl " & theBigCommand & "")


    do shell script ("curl " & theBigCommand & "")

所以我最终收到两条主要错误消息:

So I end up getting two major error messages:

curl:选项 -0800:未知:未知选项-"

curl: option -0800: is unknown: unknown option '-'

  • "无法打开 'https://zipsOnAmazon.s3-us-west-2.amazonaws.com/My Big Test.zip':没有这样的文件或目录"

    "Unable to open 'https://zipsOnAmazon.s3-us-west-2.amazonaws.com/My Big Test.zip': No such file or directory"

  • 这个错误让我感到困惑,因为我没有尝试打开该文件,而是尝试将其添加到具有该名称的路径中.但是我看到的每个 cURL 示例都有存储桶和对象名称.

    This error confuses me because I am not trying to open that file I am trying to add it to that path with that name. But every single cURL example I see has the bucket and the objectName there.

    如果有人能指出我正确的方向,我将不胜感激.提前致谢.

    If anyone could point me in the right direction that would be greatly appreciated. Thanks in advance.

    推荐答案

    你的代码中有一堆语法错误,这就是 shell 命令抛出错误的原因.大多数只是拼写错误,无疑是因为您选择在一行中写出命令字符串,并且无法跟踪开始和结束引号的下降位置.但是有一两个更大的异常.

    Your code had a bunch of syntactical errors in it, which was why the shell command was throwing errors. Most weren't really much more than typos, undoubtedly because you chose to write out your command string in one long line, and couldn't keep track of where your opening and closing quotes were falling. But there were one or two bigger anomalies.

    包含我的整个 cURL 命令的字符串都在一行中,因为我读到了最适合 AppleScript 理解的内容.

    My string that contains my entire cURL command is all in one line because I read that was best for AppleScript to understand.

    没有.

    AppleScript 不在乎任何一种方式.对于人类来说当然更难理解,但我所做的是将命令字符串发送到 AppleScript 中的剪贴板(这会解析转义字符),然后将其粘贴到 Terminal 中以查看是什么.

    AppleScript doesn't care one way or the other. It's certainly more difficult for a human to understand, but what I did was to send the command string to the clipboard in AppleScript (this parses the escaped characters), and then paste it into Terminal to see what's what.

    我将粘贴新修改的代码并尝试引导您完成更改:

    I'll paste the new modified code and try and walk you through the changes:

        set objectName to "My Big Test.zip"
    

    我删除了您对空格的手动转义,因为 AppleScript 有一种方法可以转义字符串,并且它做得很好,确保结果形式将被 shell 命令解释更正.

    I removed your manual escaping of the spaces, because AppleScript has a means by which it can escape strings and it does this very well, esuring the resulting form will be interpreted corrected by a shell command.

        set dateFormatted to do shell script "date -R"
        set s3Bucket to "zipsOnAmazon"
        set fileName to zipFile
        set relativePath to (" " & s3Bucket & "/" & objectName & " ")
        set s3AccessKey to "my-key"
        set s3SecretKey to "my-secret"
        set contentType to "application/octet-stream"
        set stringToSign to "PUT " & contentType & " " & dateFormatted & relativePath & " "
    

    我删除了命令字符串的结尾部分,并使用它们创建了一个包含 curl 的 URL 的变量.在这个串联中是现在未转义的 objectName.相反,我们将让 AppleScript 为我们处理整个 URL,而不仅仅是一个单独的组件.

    I removed the end components of your command string and used them to create a variable that contains the URL for curl. Within this concatenation is the now-unescaped objectName. Instead, we'll get AppleScript to treat the entire URL for us, rather than just one individual component.

        set OPTURL to "https://" & s3Bucket & ".s3-us-west-2.amazonaws.com/" & objectName
    

    这是最大的错误之一.您将现在的 signatureCommand 字符串合并到您的 curl 命令字符串中.这样做 是可能的,但是 a) 你做错了;b) 没有必要让自己变得比必要的更困难.

    This was one of the big errors. You incorporated what is now the signatureCommand string into your curl command string. It is possible to do this, but a) you did it wrongly; and b) there's no point making it more difficult for yourself than is necessary.

    最好先处理 echo 命令到 opensslbase64 命令,以获得签名.

    The echo command through to the openssl and base64 commands are best handled first, in order to obtain the signature.

    这也是你抛出第一个错误的地方(curl: option -0800: is unknown: unknown option '-'):这是一个简单的错字,你在那里写了 - binary 而不是 -binary,

    This is also where your first error was thrown (curl: option -0800: is unknown: unknown option '-'): it was simple typo, where you wrote - binary instead of -binary,

        set signatureCommand to "echo -en " & stringToSign & ¬
            " | openssl sha1 -hmac " & s3SecretKey & " -binary | base64"
    
        set signature to do shell script signatureCommand
    

    signature 变量现在本身就是一个 shell 命令的结果.但现在是正确的结果.

    The signature variable is now, itself, the result of a shell command. But it's now the correct result.

    接下来,我将字符串格式化为多行以使其更易于阅读.AppleScript 对此非常满意.我删除了你到处都有的无数反斜杠——它们是多余的.我还把你的反斜杠双引号换成了单引号.两者都可以接受,但后者使其更具可读性,而不会混淆双引号的开始和结束位置.

    Next, I've formatted the string over multiple lines to make it more human-readable. AppleScript is very content with this. I removed all the myriad backslashes you had all over the place—they were redundant. I also exchanged your backslashed-double quotes for single quotes. Either are acceptable, but the latter made it much more readable without confusing where double-quotes were starting and ending.

    在我排列标题之后,很明显丢失双引号是问题所在:您的标题将有一个开始的引号,但没有结束的引号.因此,下一个开始标记被认为是前一个结束标记,这扰乱了 curl 命令中的所有文本字符串.

    After I lined up the headers, it was clear that losing track of double-quotes was the issue: your headers would have an opening quotation mark, but not have a closing one. Therefore, the next opening mark was taken to be the previous closing mark, which upset all the text strings inside the curl command.

    这是格式正确的声明:

        set theBigCommand to "-X PUT -T " & quoted form of fileName & ¬
            " -H 'Host: " & s3Bucket & ".s3-us-west-2.amazonaws.com'" & ¬
            " -H 'Date: " & dateFormatted & "'" & ¬
            " -H 'Content-Type: " & contentType & "'" & ¬
            " -H 'Authorization: AWS " & s3AccessKey & ":" & signature & "'" & ¬
            " --url " & quoted form of OPTURL
    

    请注意使用称为字符串的引用形式 的属性.这是 AppleScript 转义字符串以在 shell 命令中使用它们的方式.大多数情况下,这是让 AppleScript 用单引号将字符串括起来的简单情况,这就是 URL 所需的全部内容.我还将这种格式应用于 fileName 变量,现在它的名称中可以有空格而无需打扰.

    Note the use of a proprety called the quoted form of a string. This is the way AppleScript escapes strings for using them inside shell commands. Most of the time, it's a simple case of letting AppleScript surround a string in single quotes, which is all that was required for the URL. I also applied this formatting to the fileName variable, which can now have spaces in its names without bother.

        display dialog ("curl " & theBigCommand & "")
    
        do shell script ("curl " & theBigCommand & "")
    

    我运行了 curl 命令并收到了这个输出:

    I ran the curl command and received this output:

    <?xml version="1.0" encoding="UTF-8"?><Error><Code>HttpVersionNotSupported</Code><Message>不支持指定的HTTP版本.</消息><的requestId> C413DB82E1D5502B</的requestId>< HOSTID> p4nc6t/hwGE5l29 + y3wR/VUgStZqtjMi9tem/0yl3zSCx1FyeJ/8FZW7ZFEZPYVPxkMF9wm54IxOaPGeVcprpGBrc1suEZKU</HOSTID></错误>

    这是部分预期的,因为我使用 duff 占位符作为真实文件名和访问令牌.我不确定如何解释有关不支持 HTTP 版本的消息.我建议您使用您的实际数据来尝试代码,看看服务器返回给您什么.但这至少证实了 curl 命令现在可以正常工作.

    which is partly expected, given I was using duff placeholders for real filenames and access tokens. I'm not sure how to interpret the message regarding the HTTP version not being supported. I suggest you use your actual data to give the code a try and see what the server returns back to you. But this, at least, confirms that the curl command is now working appropriately.

    如果你最终陷入服务器端的问题,我遇到了 这个 PHP 脚本 似乎完成了与您尝试执行的任务相同的任务.不知道大家是否熟悉PHP;您实际上并不需要如此,因为阅读和解释正在发生的事情非常简单.

    If you do end up getting stuck on the server side of things, I came across this PHP script that appears to accomplish the same task as you're attempting to do. I don't know whether or not you're familiar with PHP; you don't really need to be, as it's quite straightforward to read and interpret what's happening.

    如果您的脚本失败,您可以尝试模仿该 PHP 脚本中的步骤并创建适当的 AppleScript 命令以达到相同的效果.

    If your script fails, you could try mimicking the steps in that PHP script and creating the appropriate AppleScript commands to the same effect.

    这篇关于AppleScript 为 Amazon s3 执行 shell 脚本 cURL 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆