Azure Android Blob 上传 IOException

Azure Android Blob Upload IOException

我一直在尝试将 azure blob 存储与我的 android 应用程序集成,但一直遇到问题。我正在尝试将文本文件上传到服务器,但出现 IOException 错误和以下日志:

getTopLevelResources: /data/app/com.tmacstudios.topmeme-1/base.apk / 1.0 running in com.tmacstudios.topmeme rsrc of package com.tmacstudios.topmeme
09-10 14:40:45.302 16945-16945/com.tmacstudios.topmeme I/InjectionManager: Inside getClassLibPath + mLibMap{0=, 1=}
09-10 14:40:45.302 16945-16945/com.tmacstudios.topmeme D/ResourcesManager: For user 0 new overlays fetched Null
09-10 14:40:45.312 16945-16945/com.tmacstudios.topmeme I/InjectionManager: Inside getClassLibPath caller 
09-10 14:40:45.322 16945-16945/com.tmacstudios.topmeme W/System: ClassLoader referenced unknown path: /data/app/com.tmacstudios.topmeme-1/lib/arm64
09-10 14:40:45.802 16945-16945/com.tmacstudios.topmeme W/System: ClassLoader referenced unknown path: /data/app/com.tmacstudios.topmeme-1/lib/arm64
09-10 14:40:45.812 16945-16945/com.tmacstudios.topmeme D/InjectionManager: InjectionManager
09-10 14:40:45.812 16945-16945/com.tmacstudios.topmeme D/InjectionManager: fillFeatureStoreMap com.tmacstudios.topmeme
09-10 14:40:45.812 16945-16945/com.tmacstudios.topmeme I/InjectionManager: Constructor com.tmacstudios.topmeme, Feature store :{}
09-10 14:40:45.812 16945-16945/com.tmacstudios.topmeme I/InjectionManager: featureStore :{}
09-10 14:40:46.062 16945-16945/com.tmacstudios.topmeme W/ResourcesManager: getTopLevelResources: /data/app/com.tmacstudios.topmeme-1/base.apk / 1.0 running in com.tmacstudios.topmeme rsrc of package com.tmacstudios.topmeme
09-10 14:40:46.062 16945-16945/com.tmacstudios.topmeme W/ResourcesManager: getTopLevelResources: /data/app/com.tmacstudios.topmeme-1/base.apk / 1.0 running in com.tmacstudios.topmeme rsrc of package com.tmacstudios.topmeme
09-10 14:40:46.162 16945-16945/com.tmacstudios.topmeme W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-10 14:40:46.322 16945-16945/com.tmacstudios.topmeme E/TopMeme: successful connection to server
09-10 14:40:46.322 16945-16945/com.tmacstudios.topmeme E/TopMeme: upload beginning for clueful_log.txt @ /storage/emulated/0/clueful_log.txt
09-10 14:40:46.332 16945-16945/com.tmacstudios.topmeme E/TopMeme: File size: 111394
09-10 14:40:46.332 16945-16945/com.tmacstudios.topmeme E/TopMeme: available: 111394
09-10 14:40:46.342 16945-16995/com.tmacstudios.topmeme I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
09-10 14:40:46.342 16945-16995/com.tmacstudios.topmeme I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err: java.io.IOException
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.microsoft.azure.storage.core.Utility.initIOException(Utility.java:592)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.microsoft.azure.storage.blob.BlobOutputStream.close(BlobOutputStream.java:309)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.microsoft.azure.storage.blob.CloudBlockBlob.upload(CloudBlockBlob.java:679)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.microsoft.azure.storage.blob.CloudBlockBlob.upload(CloudBlockBlob.java:595)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.tmacstudios.topmeme.MainActivity.uploadFile(MainActivity.java:163)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.tmacstudios.topmeme.MainActivity.onCreate(MainActivity.java:84)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at android.app.Activity.performCreate(Activity.java:6876)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at android.app.ActivityThread.access00(ActivityThread.java:222)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at android.os.Looper.loop(Looper.java:158)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7229)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
09-10 14:40:46.472 16945-16945/com.tmacstudios.topmeme W/System.err: Caused by: com.microsoft.azure.storage.StorageException: Network operations may not be performed on the main thread.
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:188)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.microsoft.azure.storage.blob.CloudBlockBlob.commitBlockList(CloudBlockBlob.java:324)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.microsoft.azure.storage.blob.BlobOutputStream.commit(BlobOutputStream.java:339)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.microsoft.azure.storage.blob.BlobOutputStream.close(BlobOutputStream.java:306)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:    ... 16 more
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err: Caused by: android.os.NetworkOnMainThreadException
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at libcore.io.IoBridge.recvfrom(IoBridge.java:549)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:481)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at java.net.PlainSocketImpl.access[=11=]0(PlainSocketImpl.java:37)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.okio.Okio.read(Okio.java:140)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.okio.AsyncTimeout.read(AsyncTimeout.java:211)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.okio.RealBufferedSource.exhausted(RealBufferedSource.java:70)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.internal.http.HttpConnection.isReadable(HttpConnection.java:165)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.Connection.isReadable(Connection.java:1524)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.OkHttpClient.isReadable(OkHttpClient.java:91)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:475)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:465)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:447)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:353)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:468)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:     at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:103)
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme W/System.err:    ... 19 more
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme E/TopMeme: upload failed: java.io.IOException
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme D/Activity: performCreate Call Injection manager
09-10 14:40:46.482 16945-16945/com.tmacstudios.topmeme I/InjectionManager: dispatchOnViewCreated > Target : com.tmacstudios.topmeme.MainActivity isFragment :false
09-10 14:40:46.502 16945-16945/com.tmacstudios.topmeme D/SecWifiDisplayUtil: Metadata value : SecSettings2
09-10 14:40:46.502 16945-16945/com.tmacstudios.topmeme D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{8ada019 I.E...... R.....ID 0,0-0,0}
09-10 14:40:46.512 16945-17007/com.tmacstudios.topmeme D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
09-10 14:40:46.562 16945-16945/com.tmacstudios.topmeme V/ActivityThread: updateVisibility : ActivityRecord{50af8de token=android.os.BinderProxy@e66a3c9 {com.tmacstudios.topmeme/com.tmacstudios.topmeme.MainActivity}} show : true
09-10 14:40:46.652 16945-17007/com.tmacstudios.topmeme D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
09-10 14:40:46.672 16945-17007/com.tmacstudios.topmeme D/libEGL: eglInitialize EGLDisplay = 0x7f8747f178
09-10 14:40:46.672 16945-17007/com.tmacstudios.topmeme I/OpenGLRenderer: Initialized EGL, version 1.4

                                                                         [ 09-10 14:40:46.672 16945:17007 D/         ]
                                                                         ro.exynos.dss isEnabled: 0
09-10 14:40:46.682 16945-17007/com.tmacstudios.topmeme D/mali_winsys: new_window_surface returns 0x3000,  [1440x2560]-format:1
09-10 14:40:46.692 16945-16945/com.tmacstudios.topmeme W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
09-10 14:40:46.752 16945-16945/com.tmacstudios.topmeme I/InjectionManager: dispatchCreateOptionsMenu :com.tmacstudios.topmeme.MainActivity
09-10 14:40:46.752 16945-16945/com.tmacstudios.topmeme I/InjectionManager: dispatchPrepareOptionsMenu :com.tmacstudios.topmeme.MainActivity
09-10 14:40:46.752 16945-16945/com.tmacstudios.topmeme D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
09-10 14:40:46.752 16945-16945/com.tmacstudios.topmeme I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@e66a3c9 time:331880753

这是我调用它的函数。我可以确认文件路径存在:

void uploadFile(String filePath, String name){
        Log.e("TopMeme","upload beginning for "+name+" @ "+filePath);
        try
        {
            // Retrieve storage account from connection-string.
            CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.getContainerReference("memecontainer");

            // Create or overwrite the "myimage.jpg" blob with contents from a local file.
            CloudBlockBlob blob = container.getBlockBlobReference(name);
            File source = new File(filePath);
            Log.e("TopMeme","File size: "+source.length());
            FileInputStream fileInputStream = new FileInputStream(source);
            Log.e("TopMeme","available: "+fileInputStream.available());
            blob.upload(fileInputStream, source.length());
            Log.e("TopMeme","upload function completed");
        }
        catch (Exception e)
        {
            // Output the stack trace.
            e.printStackTrace();
            Log.e("TopMeme","upload failed: "+e);
        }
    }

所以事实证明,在 Azure 中,您无法在主 android 线程上做任何网络明智的事情,所以您必须在另一个线程上做。这是我的新功能:

void uploadFile(final String filePath,final String name){
        Log.e("TopMeme","upload beginning for "+name+" @ "+filePath);

        //cant perform network tasks on main thread
        AsyncTask<Void,Void,Void> task = new AsyncTask<Void,Void,Void>(){
            @Override
            protected Void doInBackground(Void... params) {
                try
                {
                    // Retrieve storage account from connection-string.
                    CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

                    // Create the blob client.
                    CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

                    // Retrieve reference to a previously created container.
                    CloudBlobContainer container = blobClient.getContainerReference("memecontainer");

                    /*
                    //create blob if it doesn't exist - hopefully resolves bugs
                    container.createIfNotExists();

                    // Create a permissions object.
                    BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

                    // Include public access in the permissions object.
                    containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);

                    // Set the permissions on the container.
                    container.uploadPermissions(containerPermissions);
                    */

                    // Create or overwrite the "myimage.jpg" blob with contents from a local file.
                    CloudBlockBlob blob = container.getBlockBlobReference(name);
                    File source = new File(filePath);
                    Log.e("TopMeme","File size: "+source.length());
                    FileInputStream fileInputStream = new FileInputStream(source);
                    Log.e("TopMeme","available: "+fileInputStream.available());
                    blob.upload(fileInputStream, source.length());
                    Log.e("TopMeme","upload function completed");
                }
                catch (Exception e)
                {
                    // Output the stack trace.
                    e.printStackTrace();
                    Log.e("TopMeme","upload failed: "+e);
                }
                return null;
            }
        };

        task.execute();
    }