i have a piece of code where the code is read adn ...
# lucee
g
i have a piece of code where the code is read adn then written back by encypting, i am trying to convert that in a function, the image is stored in the amazon s3, i am getting bit confused on this, please guide me on this
Copy code
<cffile file="#arguments.pathS3##uploadedfile#" action="readbinary" variable="myData">
encrypted=encryptBinary(myData, theKey, 'AES');
<cffile file="#arguments.pathS3##uploadefile#" action="write" output="#encrypted#" />
i converted it to a function, but i am confused on the last part where it is writing back to the s3 bucket
Copy code
public function fetchAndWrite(any resources) {
        var checkForFile = objectFileExists(resources.clientID,resources.ifile,resources.location,resources.secure);
        if(checkForFile) {
            results = fetchFile(clientID=resources.clientID,rawFile="1",gameSheet="#resources.ifile#");
        }
        if(resources.read) {
            cffile( variable="myData", file="#results#", action="info" );
            cffile( output="#encrypted#", file="#results#", action="write" );
            
        }
        return myData;
    }
or should i use amazons3 like renameobject of amazonsdk
a
Cheers for including your code (and formatting it). You are a bit light on detail re "i am confused on the last part". Confused about what? I think also the code you posted above doesn't work, irrespective of the ideal approach to take. You should make sure when writing code that at every point it works, even if "it works" does not fulfil the complete requirement. As for what is the ideal way to do something? Don't worry about that until you have some (working) way of doing it. Even if you don't write tests, you should at least work on a test-case by test-case basis. In this situation, the first case might be "it reads and returns file info of an existing file". Write that code. Don't do anything else until that works. Next case is probably "it [does something] (probably throws a FileNotFoundException) if the file does not exist". Make sure that whilst writing that step, the first step doesn't break (this is why writing tests up front saves a person from nasty surprises). I am pretty sure you have not implemented this part of it properly. Next case... it encrypts the file [same cycle as above] Next case... it writes the encrypted data back [same cycle as above] And those latter two are just happy path situations. What could go wrong with these? What happens if the read or write or encryption fails? Do you need to handle it (somehow: yes)? Do you need to make sure other code does not run in these situations? Almost certainly. My recommendation is to approach the work in an iterative fashion, nailing each bit as you go. You have raced too far ahead here, and this is why things are not going according to plan. "slow down to speed up" / "more haste, less speed" / "slow and steady wins the race" exist as idioms / cliches for a reason. You have a few problems with you code I think. If the
checkForFile
is false, why are you even continuing? Is that right? If the file isn't there, you want to halt and throw an exception don't you? You are not
var
-ing all your variables. If
resources.read
is false, where does
myData
come from? You don't need to quote-and-pound your parameters.
file=results
is fine.
file="#results#"
is just more typing, and less clear. So my guidance: Start again, slow down, work iteratively, consider using automated tests, nail each bit first, then move onto the next bit once the preceding bit is working. Make sure all earlier bits continue to work as expect whilst tinkering with subsequent bits. Oh, and don't leave us guessing about things like "i am confused". Articulate yourself more thoroughly. Although this is a big improvement on some of your erstwhile questions, so well done for working on it.
z
another way of saying some of what Adam is saying, is re-read your post before pressing send and ask yourself, how would i answer this question? if you think you would ask for more details, then provide that detail. Sometimes you'll find that in doing that, you'll solve the problem yourself
💯 1
a
This is really good advice. It's a variant of "measure twice, cut once".
g
yeah i know petty little things, which i do in last once my code starts working, y my question is based mainly on the cffile write as i have to use amazons3 to do it