7

I am new to S3 and need to use it for image storage. I found a half dozen versions of an s2wrapper for cf but it appears that the only one set of for v4 is one modified by Leigh

https://gist.github.com/Leigh-/26993ed79c956c9309a9dfe40f1fce29

Dropped in the com directory and created a "test" page that contains the following code:

s3 = createObject('component','com.S3Wrapper').init(application.s3.AccessKeyId,application.s3.SecretAccessKey);

but got the following error : enter image description here

So I changed the line 37 from

variables.Sv4Util = createObject('component', 'Sv4').init(arguments.S3AccessKey, arguments.S3SecretAccessKey);

to

variables.Sv4Util = createObject('component', 'Sv4Util').init(arguments.S3AccessKey, arguments.S3SecretAccessKey);

Now I am getting:enter image description here

I feel like going through Leigh code and start changing things is a bad idea since I have lurked here for year an know Leigh's code is solid.

Does any know if there are any examples on how to use this anywhere? If not what I am doing wrong. If it makes a difference I am using Lucee 5 and not Adobe's CF engine.

UPDATE :

I followed Leigh's directions and the error is now gone. I am addedsome more code to my test page which now looks like this :

<cfscript>
    s3 = createObject('component','com.S3v4').init(application.s3.AccessKeyId,application.s3.SecretAccessKey);

    bucket = "imgbkt.domain.com";
    obj = "fake.ping";
    region = "s3-us-west-1"

    test = s3.getObject(bucket,obj,region);
    writeDump(test);
    test2 = s3.getObjectLink(bucket,obj,region);
    writeDump(test2);

    writeDump(s3);
</cfscript>

Regardless of what I put in for bucket, obj or region I get :enter image description here

JIC I did go to AWS and get new keys:enter image description here

Leigh if you are still around or anyone how has used one of the s3Wrappers any suggestions or guidance?

UPDATE #2: Even after Alex's help I am not able to get this to work. The Link I receive from getObjectLink is not valid and getObject never does download an object. I thought I would try the putObject method

test3 = s3.putObject(bucketName=bucket,regionName=region,keyName="favicon.ico");
writeDump(test3);

to see if there is any additional information, I received this :enter image description here

I did find this article https://shlomoswidler.com/2009/08/amazon-s3-gotcha-using-virtual-host.html but it is pretty old and since S3 specifically suggests using dots in bucketnames I don't that it is relevant any longer. There is obviously something I am doing wrong but I have spent hours trying to resolve this and I can't seem to figure out what it might be.

Lance
  • 3,193
  • 2
  • 32
  • 49
  • 1
    Bad naming on my part (to differentiate the signature 4 version). It requires two components: the S3 wrapper and Sv4 utility. A) Save [The S3 Component](https://gist.githubusercontent.com/Leigh-/26993ed79c956c9309a9dfe40f1fce29/raw/da1caf605370705a7d258fcddbcb79994af60162/S3Wrapper.cfc) as `S3v4.cfc` and B) [The Signature 4 Utility](https://gist.github.com/Leigh-/a2798584b79fd9072605a4cc7ff60df4#file-sv4util-cfc) as `Sv4.cfc` (both in your `com` directory). Then add `com` to the CFC path on line 38: `variables.Sv4Util = createObject('component', 'com.Sv4').init(...);` – Leigh Jun 21 '17 at 05:58
  • .. (cont'd) Thanks for the heads up! I will fix things (so it is more clear) tomorrow. – Leigh Jun 21 '17 at 05:59
  • What exactly is the problem? What are your expected results for which function calls? – Alex Jun 24 '17 at 18:16
  • I am expecting to get the object itself (d/l) from the first call, get the link from the second call or get some type of error neither of which I am getting – Lance Jun 25 '17 at 01:20
  • After spending too much time on this I have been able to use all of the built in S3 services in Lucee to do what I need to do – Lance Jun 29 '17 at 17:54

1 Answers1

1

I will give you a rundown of what the code does:

getObjectLink returns a HTTP URL for the file fake.ping that is found looking in the bucket imgbkt.domain.com of region s3-us-west-1. This link is temporary and expires after 60 seconds by default.

getObject invokes getObjectLink and immediately requests the URL using HTTP GET. The response is then saved to the directory of the S3v4.cfc with the filename fake.ping by default. Finally the function returns the full path of the downloaded file: E:\wwwDevRoot\taa\fake.ping

To save the file in a different location, you would invoke:

downloadPath = 'E:\';
test = s3.getObject(bucket,obj,region,downloadPath);
writeDump(test);

The HTTP request is synchronous, meaning the file will be downloaded completely when the functions returns the filepath.

If you want to access the actual content of the file, you can do this:

test = s3.getObject(bucket,obj,region);

contentAsString = fileRead(test); // returns the file content as string
// or
contentAsBinary = fileReadBinary(test); // returns the content as binary (byte array)

writeDump(contentAsString);
writeDump(contentAsBinary);

(You might want to stream the content if the file is large since fileRead/fileReadBinary reads the whole file into buffer. Use fileOpen to stream the content.

Does that help you?

Alex
  • 7,743
  • 1
  • 18
  • 38
  • I have added "Update #2" to my original question. I still think there is something I am doing wrong and after trying a putObject I am getting back more information to support that. – Lance Jun 27 '17 at 00:15