kidustiliksew
kidustiliksew

Reputation: 463

Getting FileNotFoundException when trying to upload File to Google Cloud Storage

I'm trying to upload an image from Android to Cloud Storage. I'm following this official guide on how to upload files to Google Cloud Storage using the JSON API. Here is my code

private class uploadImage extends AsyncTask<File, Void, String> {

    File file = mPhotoFile;

    private String delimiter = "--";

    @Override
    protected void onPreExecute() {
        Toast.makeText(getActivity(), mPhotoFile.getPath(), Toast.LENGTH_SHORT).show();
    }

    @Override
    protected String doInBackground(File... params) {


        try {
            URL url = new URL("https://www.googleapis.com/upload/storage/v1/b/backend-images/o?uploadType=media&name=myObject?key=my_key");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setRequestMethod("POST");
            urlConnection.setChunkedStreamingMode(0);
            urlConnection.setRequestProperty("Content-Type", "image/jpeg");
            urlConnection.setRequestProperty("Content-Length", String.valueOf(mPhotoFile.getPath().getBytes().length));
            urlConnection.setRequestProperty("Authorization", "my_key");
            urlConnection.setDoOutput(true);
            urlConnection.setDoOutput(true);


            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            InputStream responseStream = new BufferedInputStream(urlConnection.getInputStream());
            BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));

            out.write(("Content-Type: image/jpeg\r\n").getBytes());
            out.write(("Content-Length: " + String.valueOf(mPhotoFile.getPath().getBytes().length)).getBytes());
            out.write("\r\n".getBytes());
            out.write(mPhotoFile.getPath().getBytes());
            out.write("\r\n".getBytes());

            String line = "";
            StringBuilder stringBuilder = new StringBuilder();

            while((line = responseStreamReader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }

            String response = stringBuilder.toString();

            Log.i("CloudStorage", response);
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }

        return "Everything was a success";
    }


}

I'm using the Public API access method and appending the Api key to the link like the guide says I could to authorize requests

Here is the error i'm getting

05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err:    java.io.FileNotFoundException:https://www.googleapis.com/upload/storage/v1/b/backend-images/o?uploadType=media&name=myObject?key=my_key
05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err:     at com.example.kid.uimockup.HomeFragment$uploadImage.doInBackground(HomeFragment.java:636)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at com.example.kid.uimockup.HomeFragment$uploadImage.doInBackground(HomeFragment.java:605)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:     at java.lang.Thread.run(Thread.java:818)

I don't have a clue if this is a problem on the client side or the server side

Upvotes: 0

Views: 646

Answers (1)

kidustiliksew
kidustiliksew

Reputation: 463

I got it work after making a few changes. I was getting a 401 Unauthorized Error code which means I didn't have authorization to access the bucket.

So instead of appending the query parameter key=api_key, i appended access_token=auth_token to authorize requests.

I then added allUsers permission to my bucket (making it public for everyone to write and read) and it worked.

Upvotes: 1

Related Questions