I need to upload a file using google cloud endpoints to google cloud storage and print back a url to the file.
I do not want to run an independent servlet handling the file uploads.
the server code looks like :
import java.io.File;
public void saveFile(File upload,User auth) throws IOException {
if (auth!=null){
String bucketName = "app-id.appspot.com";
GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()
.initialRetryDelayMillis(10)
.retryMaxAttempts(10)
.totalRetryPeriodMillis(15000)
.build());
String sname = upload.getName();
String extension = sname.substring(sname.lastIndexOf('.'),sname.length());
String sctype = URLConnection.guessContentTypeFromName(upload.getName());
String filename;
filename = String.valueOf(Calendar.getInstance().getTimeInMillis()) + extension;
GcsFilename gcsfileName = new GcsFilename(bucketName, filename);
GcsFileOptions options = new GcsFileOptions.Builder()
.acl("public-read").mimeType(sctype).build();
GcsOutputChannel outputChannel =
gcsService.createOrReplace(gcsfileName, options);
InputStream stream = new FileInputStream(upload);
copy(stream, Channels.newOutputStream(outputChannel));
}
}
private static final int BUFFER_SIZE = 2 * 1024 * 1024;
private void copy(InputStream input, OutputStream output) throws IOException {
try {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = input.read(buffer);
while (bytesRead != -1) {
output.write(buffer, 0, bytesRead);
bytesRead = input.read(buffer);
}
} finally {
input.close();
output.close();
}
}
But when i rebuild the project, the import java.io.File
gets converted into com.backend.managerApi.model.File
in the generated client libraries.
So, is there a way to do this or we will just have to run an independent servlet to handle uploads ?
Just in Case if anyone wants the Servlet code to do the same :
And Just hit the servlet like this :
curl -F file=@"picture.jpg" http://myAppEngineProj.appspot.com/myServlet