使用ColdFusion 2016 CFC上传图像AWS S3

时间:2017-03-01 23:42:31

标签: amazon-web-services amazon-s3 coldfusion

我正在尝试将图像上传到AWS S3 Bucket,但我正在使用的方法是说我的密钥和访问代码导致SignatureDoesNotMatch。我正在使用这个例子作为我的参考点。 https://gist.github.com/atuttle/4658165先谢谢你的帮助!

<cffunction name="uploadToAmazonS3">
    <cfargument name="fileName" required="true" />
    <cfargument name="contentType" required="true" />
    <cfargument name="data" required="true" />
    <cfargument name="acl" default="public-read" />
    <cfargument name="storageClass" default="STANDARD" />
    <cfargument name="HTTPtimeout" default="300" />
    <cfargument name="bucket" default="#getProperty('EmailAttachmentS3Bucket')#" />
    <cfargument name="accessKeyId" default="#getProperty('EmailAttachmentS3AccessKeyId')#" />
    <cfargument name="secretKey" default="#getProperty('EmailAttachmentS3SecretKey')#" />

    <cfset var dateTimeString = GetHTTPTimeString(Now())>

    <!--- authorization stuff --->
    <cfset var cs = "PUT\n\n#arguments.contentType#\n#dateTimeString#\nx-amz-acl:#arguments.acl#\nx-amz-storage-class:#arguments.storageClass#\n/#arguments.bucket#/#arguments.fileName#">
    <cfset var signature = createSignature(cs, arguments.secretKey)>

    <cfset var url = "http://s3.amazonaws.com/#arguments.bucket#/#arguments.fileName#" />

    <cfhttp method="PUT" url="#url#" timeout="#arguments.HTTPtimeout#">
        <cfhttpparam type="header" name="Authorization" value="AWS #arguments.accessKeyId#:#signature#">
        <cfhttpparam type="header" name="Content-Type" value="#arguments.contentType#">
        <cfhttpparam type="header" name="Date" value="#dateTimeString#">
        <cfhttpparam type="header" name="x-amz-acl" value="#arguments.acl#">
        <cfhttpparam type="header" name="x-amz-storage-class" value="#arguments.storageClass#">
        <cfhttpparam type="body" value="#arguments.data#">
    </cfhttp>
</cffunction>

<cffunction name="HMAC_SHA1" returntype="binary" access="private" output="false" hint="NSA SHA-1 Algorithm">
    <cfargument name="signKey" type="string" required="true" />
    <cfargument name="signMessage" type="string" required="true" />

    <cfset var jMsg = JavaCast("string",arguments.signMessage).getBytes("iso-8859-1") />
    <cfset var jKey = JavaCast("string",arguments.signKey).getBytes("iso-8859-1") />
    <cfset var key = createObject("java","javax.crypto.spec.SecretKeySpec") />
    <cfset var mac = createObject("java","javax.crypto.Mac") />

    <cfset key = key.init(jKey,"HmacSHA1") />
    <cfset mac = mac.getInstance(key.getAlgorithm()) />
    <cfset mac.init(key) />
    <cfset mac.update(jMsg) />

    <cfreturn mac.doFinal() />
</cffunction>

<cffunction name="createSignature" returntype="string" access="public" output="false">
    <cfargument name="in" required="true" />
    <cfargument name="secretKey" required="true" />

    <!--- Replace "\n" with "chr(10) to get a correct digest --->
    <cfset var fixedData = replace(arguments.in,"\n", chr(10), "all") />
    <!--- Calculate the hash of the information --->
    <cfset var digest = HMAC_SHA1(arguments.secretKey,fixedData) />
    <!--- fix the returned data to be a proper signature --->
    <cfset var signature = ToBase64(digest) />

    <cfreturn signature />
</cffunction>

0 个答案:

没有答案