Hii I want to store the image file in s3bucket via...
# box-products
r
Hii I want to store the image file in s3bucket via API call and I write code for it
Copy code
/**
 * I am a new handler
 * Implicit Functions: preHandler, postHandler, aroundHandler, onMissingAction, onError, onInvalidHTTPMethod
 */
component extends="coldbox.system.RestHandler"{

	this.prehandler_only 	= "";
	this.prehandler_except 	= "";
	this.posthandler_only 	= "";
	this.posthandler_except = "";
	this.aroundHandler_only = "";
	this.aroundHandler_except = "";
	this.allowedMethods = {};

	property name="s3sdk" inject="AmazonS3@s3sdk";

	/**
	 * Display a listing of the resource
	 */
	function index( event, rc, prc ){
	
		if (structKeyExists(rc, "image") && isImageFile(rc.image)) {

			var fileName = createUUID() & "." & getExtension(rc.image);

			s3sdk.putObject(
				bucket = s3sdk.getDefaultBucketName(),
				key = "coldbox/" & fileName,
				file = rc.image,
				acl = "public-read"
			);
			var imageUrl = "https://#s3sdk.getDefaultBucketName()#.<http://s3.amazonaws.com/coldbox/|s3.amazonaws.com/coldbox/>" & fileName;
			event.getResponse().addMessage( "Welcome to my ColdBox RESTFul Service" ).setData(imageUrl);
		} else {
			event.getResponse().addMessage( "Welcome to my ColdBox RESTFul Service" ).setError(true);
			
		}
	}
	
	 function isImageFile(file) {
        var allowedTypes = "image/jpeg,image/png,image/gif";
        return listFindNoCase(allowedTypes, file.getContentType());
    }

    function getExtension(file) {
        var parts = listToArray(file.getClientFile(), ".");
        return parts[len(parts)];
    }



}
but I get an error
"An exception ocurred: The function [getClientFile] does not exist in the String."
any solution or any other method to achieve this??? if yes then guide me on how to achieve.
a
So - from the error, then
var parts = listToArray(file.getClientFile(), ".");
then
file
variable is a string, not an object.
r
yes! You are right, in
rc
the
image
gets as a string that the error comes from.
👍 1
but how to overcome this I have no idea..?
a
it's a string and you want to get the file extension?
listLast(myfilename, ".")
something like that
r
Ok, when i dump the rc
writeDump(rc);
i get this 👉
a
OK - so you need to actually handle the file upload - that looks like you've just dumped out the form scope?
r
Yes, I want guidance to get the file and properties of the file and the file comes from postman request what should i do..?🤔
a
All postman (or browser) does is send the file, it is up to you to save it and store it.
Read up on fileupload link I posted
r
Ok, I understand What mistake I'm done in my program. Thanks @aliaspooryorik for your guidance. 🫡
👍 1