I have a mapped local drive (X:) that points to an S3 bucket.
I'm using the following code to push uploaded images to it.
public void copyData( MultipartFile file, String dest ) throws RecoverableFileApiException {
try {
String path = FilenameUtils.getFullPath( dest );
File fileDir = new File( path );
String fileName = FilenameUtils.getName( dest );
File outFile = new File( fileDir, fileName );
if ( !fileDir.exists() ) {
FileUtils.forceMkdir( fileDir );
}
file.transferTo(outFile);
FileUtils.touch( outFile );
//fileDir.setLastModified( System.currentTimeMillis() );
}
catch ( IOException e ) {
throw new RecoverableFileApiException( log, "There was a problem copying data file to " + dest, e, FILE_UNABLE_TO_COPY_FILE );
}
}
It creates the relevant directory (X:\uploads\category1)and put files into it on the first file upload. The file image is served correctly (Viewable from browser).
When I add new images, they are put into the same directory (X:\uploads\category1), and the image goes in ok also.
But it appears that the image is not been pushed to the edge servers.
When I view it via browser, it downloads the images instead of displaying.
In Chrome networks tab I can see it is been served as 'binary/octet-steam' content type with 0 content-length.
It's as if the aws folders need a kick at the directory level to get them to pick up the new file added to directory?
When I do the same by manually copy/paste images into directories, it servers them fine.
Use of AWS API's off the table for the moment, unless it's not possible to get working without them of course.
Will implement at later stage using API's, as suggested by John.
For now, got it working by:
- Creating file on local drive with temp name
- Copy the file to mapped drive
- Rename the file on mapped drive to proper name.
- Done, gets pushed to edge servers.
Renaming the file on mapped drive (S3 bucket) forces the new image to be served when somebody requests it next.
public void copyDataToCDN( InputStream is, String dest ) throws RecoverableFileApiException {
FileOutputStream os = null;
File tmpLocalFile = null;
try {
String path = FilenameUtils.getFullPath( dest );
File fileDir = new File( path );
String fileName = FilenameUtils.getName( dest );
File outFile = new File( fileDir, fileName );
if ( !fileDir.exists() ) {
FileUtils.forceMkdir( fileDir );
}
if ( outFile.exists() ) {
outFile.delete();
}
// Temporarily copy to local file system
String tmpLocalDir = System.getProperty( "java.io.tmpdir" );
String tmpDest = tmpLocalDir + "temp_" + fileName;
String tmpPath = FilenameUtils.getFullPath( tmpDest );
File tmpFileDir = new File( tmpPath );
String tmpFileName = FilenameUtils.getName( tmpDest );
tmpLocalFile = new File( tmpFileDir, tmpFileName );
os = new FileOutputStream( tmpLocalFile );
IOUtils.copy( is, os );
// Copy temp file to Mapped CDN Drive
String tmpCDNFileName = "temp_" + fileName;
File tmpCDNFile = new File( fileDir, tmpCDNFileName );
FileUtils.copyFile( tmpLocalFile, tmpCDNFile );
// Rename temp file on Mapped CDN Drives
File finalCDNFile = new File( fileDir, fileName );
tmpCDNFile.renameTo( finalCDNFile );
}
catch ( IOException e ) {
throw new RecoverableFileApiException( log, "There was a problem copying data file to " + dest, e, FILE_UNABLE_TO_COPY_FILE );
}
finally {
IOUtils.closeQuietly( is );
IOUtils.closeQuietly( os );
if ( tmpLocalFile != null ) {
tmpLocalFile.delete();
}
}
}