AWS无法计算MD5哈希Android

时间:2016-11-22 12:47:03

标签: java android amazon-web-services amazon-s3

我正在通过AWS java SDK将图像文件上传到S3,但我在上传时收到以下错误,我已经检查了我的凭据和我的存储桶它们没问题

Unable to calculate MD5 hash: /data/user/0/competent.groove.feetport/files/data/user/0/competent.groove.feetport/app_imageDir/IMG_20161122_073058.jpg: open failed: ENOENT (No such file or directory) 

我从相机捕捉图像并将图像保存在手机中,这是我的代码

 @Override
        public void onPictureTaken(CameraView cameraView, final byte[] data) {
            Log.d(TAG, "onPictureTaken " + data.length);
            //Toast.makeText(cameraView.getContext(), R.string.picture_taken, Toast.LENGTH_SHORT) .show();
            getBackgroundHandler().post(new Runnable() {
                @Override
                public void run() {
                    // This demo app saves the taken picture to a constant file.
                    // $ adb pull /sdcard/Android/data/com.google.android.cameraview.demo/files/Pictures/picture.jpg
                    ///storage/7A52-13E8/Android/data/competent.groove.feetport/files/Pictures/picture.jpg
                    //make a new picture file
                    File pictureFile = getOutputMediaFile();

                    if (pictureFile == null) {
                        Toast toast = Toast.makeText(getActivity(), "Picture Not saved", Toast.LENGTH_LONG);
                        toast.show();
                        return;
                    }
                    try {
                        //write the file
                        FileOutputStream fos = new FileOutputStream(pictureFile);
                        fos.write(data);
                        fos.close();
                        Toast toast = Toast.makeText(getActivity(), "Picture saved: " + pictureFile.getName(), Toast.LENGTH_LONG);
                        toast.show();

                        Log.e("-pictureFile--length-----",""+pictureFile.length());
                        Log.e("---pictureFile-----"+pictureFile.getName(),""+pictureFile.getAbsolutePath());
                        Log.e("-fileName--",""+fileName);

                        editor = sharedPref.edit();
                        editor.putString(Constants.PIC_PATH,""+fileName);
                        editor.commit();


                    } catch (FileNotFoundException e) {
                    } catch (IOException e) {
                    }



                }
            });
        }

    };




String fileName="";
    //make picture and save to a folder
    private  File getOutputMediaFile() {
        //make a new file directory inside the "sdcard" folder
       /* File mediaStorageDir = new File("/sdcard/", "JCG Camera");

        //if this "JCGCamera folder does not exist
        if (!mediaStorageDir.exists()) {
            //if you cannot make this folder return
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }*/
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();

        ContextWrapper cw = new ContextWrapper(getActivity());
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        Log.e("---directory-----", "" + directory);

        //take the current timeStamp
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        ///data/user/0/competent.groove.biometric/files/IMG_20160803_181740.jpg
        //and make a media file:

        fileName = directory + File.separator + "IMG_" + timeStamp + ".jpg";
        mediaFile = new File(directory + File.separator + "IMG_" + timeStamp + ".jpg");

        Log.e("---extStorageDirectory-----", "" + extStorageDirectory);
        //Log.e("---mediaFile-----",""+mediaFile.length());
        //Log.e("---mediaFile-----"+mediaFile.getName(),""+mediaFile.getAbsolutePath());

        return mediaFile;

    }

和我上传文件的aws代码是 公共类UploadAws扩展了AsyncTask {

private  int total, percentage = 0;
Context context;
String s3_server_path = "";
ObscuredSharedPreferences shrdpref;
ObscuredSharedPreferences.Editor editor;

String res = ""; 


public UploadAws(Context context){
    Log.e(""+getClass(), "UploadAws constructor called");
    this.context = context;
    shrdpref = new ObscuredSharedPreferences(context, context.getSharedPreferences(Constants.PREFERENCES, Context.MODE_PRIVATE));

}

@Override
protected Object doInBackground(Object... params) {
    try {
        Log.d("" + getClass(), "UploadAws doInBackground called");
        File pictures = null;
        AmazonS3Client s3Client = null;
        TransferUtility tx = null;
        percentage = 0;

    try {                           
        s3_server_path = shrdpref.getString(Constants.PIC_PATH,"");

        File dir = context.getFilesDir();
        pictures = new File(dir, s3_server_path);
        s3Client = new AmazonS3Client(new BasicAWSCredentials(shrdpref.getString(Constants.AWS_Access_Key,""),shrdpref.getString(Constants.AWS_Secret_Key,"")));
        tx = new TransferUtility(s3Client,context);

    } catch (NullPointerException e) {
        e.printStackTrace();                
    }                 

        try {

            Log.e("------File length--------=", "" + pictures.length());
            Log.e("------File getName--------=", "" + pictures.getName());
            Log.e("------File getAbsolutePath--------=", "" + pictures.getAbsolutePath());
            s3Client.setEndpoint("s3.amazonaws.com");

            String Feetport_bucket = shrdpref.getString(Constants.fs_bucket, "");

            if (s3Client.doesBucketExist(Feetport_bucket)) {
                Log.e("Warn", "service called bucket exist");
            } else {
                s3Client.createBucket(Feetport_bucket);
                Log.e("FOS signature", "Bucket created");
            }

            int imageIndex = pictures.toString().lastIndexOf("/");
            String pictureString = pictures.toString().substring(imageIndex + 1, pictures.toString().length());

            String selfie_url = "FeetPort/" + shrdpref.getString(Constants.company_name, "") + "/attend/" + Utils.getCurrentDate() + "/" + pictureString;
            Log.d("UploadAws selfie_url called: ", "" + selfie_url);

            //https://feetport.s3.amazonaws.com/8632/17/20161122/mayank1_I_1624_065610_COL22.jpeg

            final TransferObserver observer = tx.upload(Feetport_bucket, pictures.getName(), pictures);

            observer.setTransferListener(new TransferListener() {
                @Override
                public void onStateChanged(int arg0, TransferState state) {
                    Log.e("onStateChanged", "on state changed " + state);
                }

                @Override
                public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                    Log.e("onProgressChanged", "total bytes " + observer.getBytesTotal());
                    Log.e("onProgressChanged", "total bytes transfered " + observer.getBytesTransferred());
                    percentage = (int) (bytesCurrent / bytesTotal * 100);
                    Log.e("onProgressChanged", "total percentage " + percentage);
                }

                @Override
                public void onError(int arg0, Exception arg1) {
                    Log.e("onError=" + arg0, "on Error  " + arg1.toString());
                    percentage = 101;
                }
            });


            do {


            } while (percentage < 100);

            Log.e("percentage", "percentage  " + percentage);
            if (percentage == 100) {
                /**
                 * CGPL-17 9-5-2016 dynamic bucket and url.
                 */
                String S3_SERVER = "https://".concat(Feetport_bucket).concat(".s3.amazonaws.com/");
                String imageUrl = S3_SERVER + selfie_url;

                //Session.setselfie_url(shrdpref, imageUrl);

                editor = shrdpref.edit();
                editor.putString(Constants.PIC_PATH, "" + imageUrl);
                editor.commit();

                Log.e("Selfie url ", "imageUrl --- " + imageUrl);
        /*JsonParameters object = new JsonParameters(context);
        object.markedAttendance(callback);*/
            } else {
        /*JsonParameters object = new JsonParameters(context);
        object.markedAttendance(callback);*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    } catch (Exception exception) {
        exception.printStackTrace();

    }
    return null;
}

}

1 个答案:

答案 0 :(得分:0)

这与S3无关。

ENOENT (No such file or directory)是本地错误:找不到文件。

仔细查看错误消息中的路径。我在这里包含了路径,并添加了一些换行符以便于阅读。我没有编辑这个,重要的是,我没有多次粘贴它:

/data/user/0
/competent.groove.feetport
/files/data/user/0
/competent.groove.feetport
/app_imageDir/IMG_20161122_073058.jpg

我并不是说 错误,但肯定看起来错了。当然看起来你正试图从你错误构造的路径上传文件,在目录结构中有一些重复......并且没有这样的目录,没有这样的文件。