Current Thread Exception when trying to write to Storage

215 views
Skip to first unread message

Fat Cat Gaming

unread,
Apr 14, 2019, 9:24:07 AM4/14/19
to Google Cloud Developers
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!

George (Cloud Platform Support)

unread,
Apr 14, 2019, 11:26:15 AM4/14/19
to Google Cloud Developers
You may consider having a look at the solution for a similar situation, detailed on the "App Engine and Google Cloud Storage Sample" page

This discussion group is oriented more towards general opinions, trends, and issues of general nature touching the app engine. For coding and programming architecture, as well as uploading files to a GCE bucket from within a web app, you may be better served in dedicated forums such as stackoverflow, where experienced programmers are within reach and ready to help.
Reply all
Reply to author
Forward
0 new messages