GAE+JUnit: Add a blob to the blobstore - java

I'm using Google App Engine for Java, and I'm trying to write JUnit tests for my code that deals with the blobstore. How can I put a blob in the blobstore to use during testing (after it has been configured to keep everything in-memory with the LocalBlobstoreServiceTestConfig)? It seems like the solution should be very simple and basic, but somehow I cannot find it.
I imagine it was possible using the Files API, but I have steered clear of it because it is being discontinued (next week).
The only way I know of to get blobs into the blobstore outside of tests is via a multi-part form upload. However, I don't know how I would do that in the context of a JUnit test.
One can also interact with Google Cloud Storage objects via the blobstore API, but I have chosen to use the vanilla blobstore so that I don't need to loop yet another service (and its client libraries) into my app.
So again I ask, how do I get a blob in there so I can test the code that uses it?

It seems as though you're correct that this feature does not exist. You should open a Feature Request issue in the public issue tracker and any other users interested in this can star the issue. Once that's done, it will be triaged and logged Google-side for implementation over time.

Related

How can I upload videos to JWPlayer with Java (Spring Boot)

I have a Java Spring Boot web app which should allow the users to upload their videos. I did some researches and I found that JWPlayer is the best solution in this way.
I would like to upload videos to JWPlayer and store in a database the id of the uploaded video related to a particular user.
The only problem is that the documentation is not so clear, and I don't understand how can I interact with JWPlayer with my Spring Boot back end.
There is just this page which does not show any code nor give any intuition of the process.
JWPlayer doesn't provide a Java or Spring Boot integration, but it does have APIs that you can call from Java. The steps go as follows:
Create your video metadata by calling /v1/videos/create. I highly recommend using the S3 upload method. If you're not sure how to make this call you can read about the call structure and authentication
Upload your video by using the link returned in the create call. You can read more about how to do this here if you do the S3 method, or here otherwise.
There is a pretty good example of how this entire process goes written in Python on the page you linked.
If you're uploading videos on behalf of your users, it'd probably be easiest to do this on the frontend with JavaScript instead of proxying through your Java application

How do you upload a file using an app created in Android Studio to Google Cloud Storage?

Would anyone please take the time to step-by-step document this seemingly simple task?
I am new to java and the latest Google Cloud Platform APIs. I cannot figure out how to do the simple task of uploading a file to Google Cloud Platform from an Android App. Most examples are three years old or are not end-to-end. Either way, they fail so mysteriously at some point that I cannot figure out how to proceed. For example, the exception I'm getting with using the simple-cloud-storage demo is "null." I can't even figure out what is generating "null."
So far I've only been successful in doing something by using the endpoints tutorial: https://cloud.google.com/appengine/docs/python/endpoints/getstarted/clients/android/ But there is no talk about exchanging files in that demo.
Complete code example is here. This code example was created from the Google HelloEndpoints tutorial. This is probably a terrible implementation of using GCS in Android, but it works unlike most examples currently available.
Steps to achieve this:
Create a GCS bucket with service account credentials.
Get a .p12 private key and place that into src/main/res/raw. You have to remove the .p12 extension and replace the - with a _. You
need the password, too, so keep that handy.
Look in MainActivity.java at public void onClickFileChooser(View view). What this does is call a class that allows you to
navigate and select a file. There's some commented-out code that
checks with the server for an upload URL in case you want to try to
store the file with the blobstore API. I could not figure that out,
so I tried GCS. The GCS stuff is the next bit of code. You'll notice
it's a two step process. There are some globals defined for GCS
because I'm not a good programmer.
The first step is getting a "Google credential." The code loads the private key with keystore.load(getResources().openRawResource(R.raw.thomasmhardy_ebc515c808a6)
and does some other stuff. You might have to change the key
password. Look for "notasecret" and replace that with your password.
The actual storage of the file is next. You'll see the URI is set per the Google example. Next, I set up an asynchronous
task because Android does not want you doing anything on the network
in the main thread. The task first refreshes the GoogleCredential.
Once's that's complete, it uses android-async-http to post the
file to GCS. Note, the header for Authorization concatenates "Bearer
" before the access token. That uploads the file.
All this requires a few dependencies. Check out the gradle.
I also had to enable multiDex.
Note: I removed the contents of my private key "thomasmhardy_ebc515c808a6."

80mb Image/Video uploading to cloud storage / blobstore

I am a C# developer who recently decided to venture into the mobile dev world specifically android dev using java. It has been a smooth ride until now. While I'm usually able to follow the tutorials on the android developers site and other sites easily, uploading an image/video to the app engine blobstore or cloud storage seems to be a bit over my head. I cant seem to wrap my head around the process.
I have already done the necessary steps to get my app running on app engine. I am able to register devices and save basic entities to the cloud but I cant seem to get any of the tutorials to work form me.
Basically my app allows users to take images or videos and then have them posted to my back end as part of a file sharing type of game.
I am not asking for someone to give me code that can accomplish this, instead pointing me to a very thorough tutorial would be great!
1.I need understand the role of a servlet (most examples use this for uploading to blobstore)
2.Where should the servlet reside?
3.Is there another way to do this without using a .jsp page for the file uploading ?
4. I have set up the app engine back end for my project including endpoints. Is there a way to send the images/video that way to the server and still be able to retrieve a serving url to fetch the image/video later?
I have been stuck on this for a week now and I'm getting quite discouraged as I cannot seem to adapt any of the tutorials that I have read to suite my needs. There are a few dark spots in my mind where the entire process is concerned. Since I am not familiar with java, servlets etc I'm stumbling in the dark and I just need a lamp to light my path. I think i'm just a step or two away.
BTW I have looked at these questions and tried the code suggested to no avail before deciding to post my own.
Upload to Appengine Blobstore in Android
BlobStore vs Cloud Storage for storing user uploaded images
Upload image from Android to Google Cloud Storage or Google App Engine Blobstore
Please help!
NOTE: I don't have an interface where the user gets to choose the image/video as it is to be taken directly from the camera and sent to the server(not sure that makes a difference).
You need to do a lot more reading. First understand servlets in general. Then learn how to use the blobstore or cloud storage api. Then read how to post directly to blobstore (not to the servlet) with a post-save servlet callback.
Welcome to the world of Java!
I'll try my best to explain the Servlets/JSPs part. I dont know a whole lot about Google app engine.
1) Servlets are nothing but Java objects but with more priveleges. Servlets know how to handle a web request! There are many things that come to mind when we talk about web request processing:
who maps the URL to this servlet?
Who gives this servlet user request parameters?
How can I guarantee only authorized users can access this servlet?
list goes on......
Answer to all above questions is a web container. And in the world of Java only one web container rules (others might differ): Apache Tomcat.
In short, servlets are Java objects created for you by Tomcat.
Where should the servlet reside?
They reside in Tomcat.
Is there another way to do this without using a .jsp page for the file
uploading ?
You dont need a JSP to send requests to a servlet. You can use jquery, python or anything else. As long as its a valid web request.
I have set up the app engine back end for my project including
endpoints. Is there a way to send the images/video that way to the
server and still be able to retrieve a serving url to fetch the
image/video later?
See the tutorial here. Especially 'Implementing Tweet My Picture' section.
Google app engine has Java API (along with python) and the best way to submit web request to that API using Java is servlets. That's why many example are in servlets. I think.
Here's what i would do if I were you:
Setup tomcat. There are many options. So reply if you are not sure.
Deploy your handler servlet that'll use Google app engine Java API.
Send your image files to blobstore (not the servlet). See the tutorial. App engine will forward blob related information to the handler servlet you created in step 2.
Use Blobstoreservice in that servlet so that users can access that uploaded image.
Let us know if this helped.

Setting uploadType in Google Drive Java API

I'm having the same problem as this and this but in the Java domain. This question also covers what I want but since no answers have been forthcoming I thought I'd ask it here, with a little more detail.
I'm most of the way through writing a Java application to back up files to Google Drive. As others have found, 'internal server error 500' failures are a pretty common problem with uploads, but for small files, implementing the recommended exponential back-off and retry works okay. For large files, however, (anything over a few MB) the failure rate is unacceptably high. In some cases I'm getting well over 50% failure rate, which makes any long backup job effectively impossible.
When inserting (uploading) a file using the Google Drive v2 API, the documentation clearly states that three upload types are available: simple, multipart and resumable. The upload type is specified by adding a parameter to the endpoint URL. Clearly, what I'm after is the resumable upload type.
The problem
There appears to be no method to set this uploadType parameter using the API. There's a method call to set every optional parameter (as detailed here), but not a sniff of a way to set uploadType to resumable. No code snippets, no documentation, no nothing.
Somewhat confusingly, there is also what appears to be an unrelated 'chunked' media upload mode, which is the default and which I've actually disabled in my application by calling request.getMediaHttpUploader().setDirectUploadEnabled(true), since it appears to make no difference whatsoever to the reliability of an upload, no matter what the chunksize is set to, and it hugely slows uploads down.
I'm on the verge of circumventing the API and building the requests manually, but I'd really like to know if anyone else has encountered/solved this first. It's such a glaring omission that I can't believe lots of folk haven't encountered it before.
Cheers all.
David.
Short answer:
The URI passed to MediaHttpUploader's resumable upload when you use the Google Drive java client API seems to be the same as the one proposed on Google Drive API V2. So actually by default the Google Drive Java client API is already using the resumable upload.
Long answer:
After tracing the code in Google Drive Java API client all the way from
com.google.api.services.drive.Drive
->com.google.api.services.drive.DriveRequest
->com.google.api.client.googleapis.services.AbstractGoogleJsonClientRequest
You will find that Drive's constructor passes the URI to DriveRequest, which also passes the variable uriTemplate to AbstractGoogleJsonClientRequest. And finally AbstractGoogleJsonClientRequest uses buildHttpRequestUrl() to generate the resumable URI proposed in Google Drive API reference. This URI is stored in the variable httpRequestUrl in AbstractGoogleJsonClientRequest. httpRequestUrl will then be passed to uploader's upload method. This method by default (directUploadEnabled defaults to false) will use the resumable upload instead of direct upload.
There is an example on this page https://developers.google.com/gdata/docs/resumable_upload?csw=1#InitialRequestJava
Now, here's where it gets confusing. That page allegedly refers to the old style Gdata api. However the current source code at https://code.google.com/p/google-api-java-client/source/browse/google-api-client/src/main/java/com/google/api/client/googleapis/media/MediaHttpUploader.java references that page as being the API that it implements.

Force URL of local blobstore response

I am using facebook login on my site. When I test locally I need to use local.mysite.com, so facebook thinks the request is coming from my site. This works great except when I upload images to blobstore. When uploading images app engine always switches to localhost:888. This makes the browser think cross site scripting is happening and prevents my uploads. How can I force app engine to use local.mysite.com instead of localhost:888
This is the error I am getting:
XMLHttpRequest cannot load http://localhost:8888/_ah/upload/agpidWJwcm9qZWN0chsLEhVfX0Jsb2JVcGxvYWRTZXNzaW9uX18YBQw. Origin http://local.mysite.com:8888 is not allowed by Access-Control-Allow-Origin.
I'm not sure you can actually change that URL.
What you can do though is to use the localhost:8888 for your local tests and create another Facebook application that points to localhost. Afterwards there are two approaches that you can do in order be able to use these two (or possibly even more in the future) Facebook applications in your app.
You can decided based on the requested URL which key to use
Store all the keys in somekind configuration Datatstore that only admins can change them
With the first approach you will have to store somehow all the keys in your code or even worse in the datastore and then decided based on the URL which one to use. This approach is not good and it doesn't scale very well. The second approach is preferable since you don't have to store your keys in the code, it is more secure and it scales much better since you don't need to know up front how many different Facebook applications you have.
You can read the Nick Johnson's answer on how to solve that in Python, but the idea is Java so it shouldn't be that hard.

Categories

Resources