Hi guys,
I'm trying to upload some files to a bucket from within a web app. the purpose is to store the image of a product. Currently testing is being done via my machine. I found some snippets of code but have encountered issues with the ThreadManager and, to be honest, I'm not sure how best to resolve.
This is the code that i'm running:
public static void test() throws IOException {
GcsService gcsService = GcsServiceFactory.createGcsService();
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
GcsFileOptions options = new GcsFileOptions.Builder()
.mimeType(".jpg")
.acl("project-private")
.build();
String bucketName = "my_test_bucket";
String secureFilename = "https://helpx.adobe.com/uk/stock/how-to/visual-reverse-image-search/_jcr_content/main-pars/image.img.jpg/visual-reverse-image-search-v2_1000x560.jpg";
GcsFilename p_filename = new GcsFilename(bucketName, secureFilename);
GcsOutputChannel writeChannel = gcsService.createOrReplace(p_filename, options); //exception here
writeChannel.write(ByteBuffer.wrap(null/*image.getImageData()*/));
writeChannel.close();
BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + p_filename.getBucketName() + "/" + p_filename.getObjectName());
ImagesService imageService = ImagesServiceFactory.getImagesService();
ServingUrlOptions servoptions = ServingUrlOptions.Builder.withBlobKey(blobKey).secureUrl(true);
String url = imageService.getServingUrl(servoptions);
}
public static void main(String[] args) {
ThreadFactory threadFactory = ThreadManager
.currentRequestThreadFactory();
Thread thread = threadFactory
.newThread(new Runnable() {
public void run() {
try {
System.out.println("THREAD!");
test();
} catch (IOException e) {
e.printStackTrace();
}
}
}
);
thread.start();
}
With the exception here being:
Exception in thread "main" java.lang.NullPointerException: Current thread is not associated with any request and is not a background thread
at com.google.appengine.api.ThreadManager.getCurrentEnvironmentOrThrow(ThreadManager.java:107)
at com.google.appengine.api.ThreadManager.currentRequestThreadFactory(ThreadManager.java:48)
at gametest.Application.main(Application.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Process finished with exit code 1
Any help would be fantastic, thank you!