Why aren't my files showing? Using Amazon S3 with ColdFusion - coldfusion

I have the following code to put a file on to an Amazon S3 bucket and then retrieve the directory listing:
<!--- set a variable to hold our S3 directory --->
<cfset s3Dir = "s3://#access_ID#:#secret_key##bucket/folder/">
<!--- check if directory exists and create if not --->
<cfif not directoryExists(s3Dir)>
<cfset directoryCreate(s3Dir)>
</cfif>
<!--- create a random file with a random number as its content --->
<cfset fileWrite
('#s3Dir#/RandomFile_#DateFormat(Now(),'yyyymmdd')#-#TimeFormat(Now(),'HHmmss')#.txt',
'#RandRange(100000,100000000)#')>
<!--- get the directory contents --->
<cfdump var="#directoryList(s3Dir)#">
The file saves to Amazon S3 correctly and I can see it using the S3 console. But the dumped out directory contents is always an empty array. Any ideas as to why this might be?

I have just found the answer on Adobe's bug base at https://bugbase.adobe.com/index.cfm?event=bug&id=3554224
It is caused by having a trailing slash at the end of the directory path. Once I remove the trailing slash and restarted CF the file names are returned as expected.

Related

AWS S3 file with same name does not get overwrite but gets characters added at the end of filename

Below is an example for my scenario,
I have a Django API which allows user to upload images to a certain directory, the images will be stored in an S3 bucket. Let's say the file name is 'example.jpeg'
User again uploads image with the same name 'example.jpeg' to the same directory.
Both of them correctly show up in the same directory but the second one gets additional characters at the end of the filename like this 'example_785PmrM.jpeg'. I suspect the additional characters are added by s3 but my research says s3 will overwrite the file with same name.
How can I enable the overwrite feature, I haven't seen any option for this.
Thanks
S3 itself does not change a key on it's own. The only option I see that can be impacting this is Django's storage backend for S3:
AWS_S3_FILE_OVERWRITE (optional: default is True)
By default files with the same name will overwrite each other. Set this to False to have extra characters appended.
So you should set AWS_S3_FILE_OVERWRITE to True to prevent this behavior.
Depending on your exact needs, consider enabling S3 versioning so you can access previous versions of a objects as they're overwritten in S3 in the future.

can not add file in aws s3 bucket using postman

I am trying to add a file in s3-bucket in my AWS account using postman. see below screenshot.
I pass Host in the header as a divyesh.vkinds.com.s3.amazonaws.com where divyesh.vkinds.com is my bucket name. and in Body I am giving file as index.html as file type like image below.
but it is giving me The provided 'x-amz-content-sha256' header does not match what was computed.
error. I searched for it but can't find anything.
Please check content-header. Add Content-Type as text/plain and date in this format XX-XX-XXXX
I have also faced the same problem. The issue was that, postman does not calculate the SHA. It defaults to a SHA of empty string e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
So in the postman headers, add an explicit key x-amz-content-sha256. Caluclate the value of SHA256 for your file using a sha command and provide as the value. Below command works on linux flavors
shasum -a 256 index.html
Couple of other observations in the question.
You can change the Body as binary and choose the file you want to upload.
Provide the complete path including the file name in the upload URL. E.g. if you provide the URL as <your bucket name>.s3.<region>.amazonaws.com/test/index.html, the file will be copied to test directory in the bucket with name as index.html
I encountered this situation recently, and the issue was that I was copying an active log file which changed between when my side calculated the hash and when the file was actually uploaded. My solution was to copy the file to a temporary location, then upload that stable file.

Displaying image using ColdFusion from BLOB

I want to display an image from a database. I'm using data type BLOB for that image.
I've already tried #CharsetEncode(viewPoint.ppp_icons, "ASCII")#, but it didn't work.
<cfimage action="writeToBrowser" source="#imageBlob#">
http://livedocs.adobe.com/coldfusion/8/htmldocs/Tags_i_02.html
OR... Use Data URI scheme (w/ limited browser support).
<img src="data:image/png;base64,#toBase64(imageBlob)#" />
<cfcontent reset="Yes" type="image/gif" variable="#QueryName.BlobColumn#" />
EDIT: To clarify, you would place this code in a separate template. Which you would place the call for in the src attribute of your img tag. You would pass the primary key of the database table and the new template would lookup and grab the blob column to be outputted.
I believe it would be better to keep all blob data in separate requests, because if you were trying to display many files on a page it would be best to let the page load up first rather than have the page load time wait until all blob data could be downloaded from the sql server to the coldfusion server. Reducing initial page load times is of crucial importance to usability.
As an addendum, if these files are going to be accessed frequently, then it will also be best to have the file be cache on the web server and have the separate template redirect to the cached file.

how to set permissions for cfftp getfile from coldfusion code?

I am using and getting a file from ftp server. But when I use to read the downloaded file, its not allowing me to read the file. Its because of the permissions on the files.
How can i set permission to 777 or full access for that file from code. I don't want to do that manually. I am using Mac OS.
Thanks..
For setting permissions on a file in ColdFusion use the optional mode attribute of cffile with the octal values of UNIX chmod command.
<cffile action="write" destination="#fileToWrite#" mode=777>
This applies to Unix/Linux only.
If this is about files uploaded to your server and you have access to your ftp admin / config files, then you probably would want to modify the upload mask to adjust permissions of the files uploaded.
If you download the files yourself manually, then you would have to put it in a folder where coldfusion at least has read access, or tell your ftp client to store the file accessible for cf.
You can write permissions using CFFILE. I don't think there is a way to do only that, but you could do it as part of a rename or move operation. Check the docs for more specifics on it.

EXECUTE permission denied on object, database , owner 'dbo'

I successfully executed a stored procedure on a SQL2005.When I wanted to populate the results in a form via CFSTOREDPROC , I am getting the following error.
[Macromedia][SQLServer JDBC Driver][SQLServer]EXECUTE permission denied on object 'GetPSRreportStock', database 'CGTSP_GET',
<cfstoredproc procedure="FP_Get..GetStartStopTotalBalesCott" datasource="#TS#" username="#UNT#" password="#SPW#">
<cfprocparam type="in" cfsqltype="CF_SQL_INTEGER" dbvarname="#reportYear" value="#xxMDB#">
<cfprocparam type="in" cfsqltype="CF_SQL_INTEGER" dbvarname="#orderMonth" value="#xxOBDB#">
**<cfprocresult name="ccDPR">**
</cfstoredproc>
The CF code is in MX-6.
How to correct the error?
Check to make sure the user trying to run the stored proc has execute permissions, you can do this in SQL Server Management Studio by going to the Database -> Programmability -> and Right Click on the Stored Proc and select 'Properites', then go to the 'Permissions' section and see if your user is there, if its not, you can add the user and give it EXECUTE permissions, or you can do the following in a new query:
GRANT EXECUTE TO <username> ON <stored proc name>
I think it's because you need to give the user your web server is running as EXECUTE permissions for that stored procedure (in the SQL Server management console).
Go to "Properties" in each stored procedure in the SQL Microsoft management, then select "Permissions" on the left side, then add the user you use by searching for it on the Search... button, then click Execute check box. Then ok and ok.