I want to upload my file in amazon s3bucket and I'...
# box-products
r
I want to upload my file in amazon s3bucket and I'm written code for it
Copy code
function index( event, rc, prc ){
		if (structKeyExists(rc, "image") ) {
			var uploadedImage = fileUpload( getTempDirectory(), "image", "image/jpeg,image/png,image/gif", "MakeUnique" );
			fileNameWithoutSpace = replace(uploadedImage.clientfilename, " ", "_")
			var fileName = fileNameWithoutSpace & "_" &createUUID()& "." & uploadedImage.clientfileext;
			var folderName = "coldbox"; 
			var fileNameToUpload = folderName & "/" & fileName;
		
			var tes = s3sdk.putObject(
				'Bucket': s3sdk.getDefaultBucketName(),
				'uri':fileNameToUpload,
				'Body': uploadedImage,
				'ACL': 'public-read',
				'ContentType': uploadedImage.contenttype,
				'md5':'auto'
			);
			
			var imageUrl = "https://#s3sdk.getDefaultBucketName()#.<http://s3.amazonaws.com/|s3.amazonaws.com/>" & fileNameToUpload;

			event.getResponse().addMessage( "Submitted successfully" ).setData(imageUrl);
		} else {
			event.getResponse().addMessage( "Failed" ).setError(true);
			
		}
	}
Everything goes successfully but the error is i can able to see the image file that i've uploaded the response as follows
Copy code
{
  "data": "<https://srijaneducation.s3.amazonaws.com/coldbox/OIG.cLIuBy9TZIHQ4cg2cHnJ_BC68F717-700B-424D-AB6273104A4B08D8.jpg>",
  "error": false,
  "pagination": {
    "totalPages": 1,
    "maxRows": 0,
    "offset": 0,
    "page": 1,
    "totalRecords": 0
  },
  "messages": [
    "Welcome to my ColdBox RESTFul Service"
  ]
}
the image response is
<https://srijaneducation.s3.amazonaws.com/coldbox/OIG.cLIuBy9TZIHQ4cg2cHnJ_BC68F717-700B-424D-AB6273104A4B08D8.jpg>
what's the exact solution for it
a
Please use threads so that the main channel does not get swamped with blocks of code.
What is the issue exactly? You can't view that URL? So nothing to do with your upload code then?
r
Ok, but in my browser it shows a blank page why this happens i don't have any idea
a
and have you looked at the status code?
r
NO, ok let me try with it once.
the putObject() returns only a string value and the value is shown below
a
OK, so knowing how to debug things is a bit of an art but very useful. For this specific issue, open your browser developer tools, try that URI in your browser. Look at your network console and tell me what status code you get for you http request.
r
Ok this returns in my network when i load this

https://srijaneducation.s3.ap-south-1.amazonaws.com/coldbox/OIG.cLIuBy9TZIHQ4cg2cHnJ_AC86626B-4B67-4DCB-AE0BEA7388C01EDB.jpg

a
right - so you have a 200 status and a filesize of 440Bytes. So you did upload something
r
yes
Do you mean when I upload the whole file didn't upload????
a
you're not uploading what you think you are 🙂
Not sure which library you are using but you need to read the image file as binary and store that in s3.
I'm guessing you are just blindly passing
uploadedImage
into
putObject
without checking what
uploadedImage
is.
If this is the ortus coldbox module then something like this is probably what you want https://github.com/coldbox-modules/s3sdk/blob/master/models/AmazonS3.cfc#L790
p
It appears to your "messages" response from this original post that your route is just hitting the default route; do you have a view file setup for this route (index)? Something is being ignored when you are setting your
addMessage
. All this being said with the assumption that your putObject IS successfully putting that on the s3 bucket per convo above.
a
I believe this is RESTFul endpoint @Patrick so doesn't need views
r
yes this is RESTFul endpoint
a
I'm reading between the lines here though - not going to write the code, but hopefully can point in the right direction
This might be useful @Rameshwar Bhaskar - specifically for files by the look of it https://github.com/coldbox-modules/s3sdk/blob/master/models/AmazonS3.cfc#L758
That should save you having to do the binary conversion - just give it the local file path of the file you want to upload.
r
OK, @aliaspooryorik let me try if needed something I'll ask for help...
a
No problem - if things are not doing what you want then
writedump
will help a lot to understand what sort of data you have (just as you would use console.log in Javascript). As you are using ColdBox then also well worth looking into the
debug()
methods in LogBox.
r
OK, Thanks For your help @aliaspooryorik I'm achieved what i want the demo is

https://srijaneducation.s3.ap-south-1.amazonaws.com/coldbox/OIG.cLIuBy9TZIHQ4cg2cHnJ_EE275BC3-1DD7-4419-B6D6DB6A310E9268.jpg

🙌 1
🙌🏻 1
🙌🏼 1