首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google :匿名调用者没有storage.objects.create访问Google对象的权限

Google :匿名调用者没有storage.objects.create访问Google对象的权限
EN

Stack Overflow用户
提问于 2021-01-09 12:34:29
回答 1查看 1.9K关注 0票数 0

我试图将图片上传到,我得到了这个异常,我尝试过搜索这个问题,但是在android中这个问题是没有问题的,并且无法继续下去,下面是我的代码:

MainActivity.Java

代码语言:javascript
运行
复制
public class MainActivity extends AppCompatActivity {

    private String currentPhotoPath;
    private String imageName;
    public static final int REQUEST_IMAGE_CAPTURE = 1;
    private File photoFile = null;
    private String[] permissions;
    public static final int PERMISSION_REQ_CODE = 200;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView= findViewById(R.id.textView);
        permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_COARSE_LOCATION};
        acceptPermissions();

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dispatchTakePictureIntent();
            }
        });
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        try {
            photoFile = createImageFile();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this, getPackageName(), photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        String fileName = "temp";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(fileName, ".jpg");
        currentPhotoPath = image.getAbsolutePath();
        imageName = image.getName();
        return image;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try  {
                        UploadObject.uploadObject("project_id", "bucktedname", imageName, currentPhotoPath);                       
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();          
        }
    }

    private void acceptPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(getApplicationContext(), permissions[0]) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[1]) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[2]) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[3]) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[4]) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[5]) != PackageManager.PERMISSION_GRANTED)
                requestPermissions(permissions, PERMISSION_REQ_CODE);
            else {
                if ((ContextCompat.checkSelfPermission(getApplicationContext(), permissions[0]) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(getApplicationContext(), permissions[1]) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(getApplicationContext(), permissions[2]) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(getApplicationContext(), permissions[3]) != PackageManager.PERMISSION_GRANTED) || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[4]) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), permissions[5]) != PackageManager.PERMISSION_GRANTED)
                    requestPermissions(permissions, PERMISSION_REQ_CODE);
            }
        }
    }

}

UploadObject.Java

代码语言:javascript
运行
复制
public class UploadObject {
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static void uploadObject(
            String projectId, String bucketName, String objectName, String filePath) throws IOException {

        Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
        BlobId blobId = BlobId.of(bucketName, objectName);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
        storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)));
        System.out.println(
                "File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
    }
}

逻辑猫:

代码语言:javascript
运行
复制
W/System.err: com.google.cloud.storage.StorageException: Anonymous caller does not have storage.objects.create access to the Google Cloud Storage object.
        at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:232)
        at com.google.cloud.storage.spi.v1.HttpStorageRpc.create(HttpStorageRpc.java:313)
        at com.google.cloud.storage.StorageImpl$3.call(StorageImpl.java:221)
W/System.err:     at com.google.cloud.storage.StorageImpl$3.call(StorageImpl.java:218)
        at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:105)
        at com.google.cloud.RetryHelper.run(RetryHelper.java:76)
        at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:50)
        at com.google.cloud.storage.StorageImpl.internalCreate(StorageImpl.java:217)
        at com.google.cloud.storage.StorageImpl.create(StorageImpl.java:171)
W/System.err:     at com.example.googlecloudstorgae.UploadObject.uploadObject(UploadObject.java:35)
        at com.example.googlecloudstorgae.MainActivity$2.run(MainActivity.java:111)
        at java.lang.Thread.run(Thread.java:923)
W/System.err: Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
    POST https://storage.googleapis.com/upload/storage/v1/b/"bucket-Name"/o?projection=full&uploadType=multipart
W/System.err: {
      "code" : 401,
      "errors" : [ {
        "domain" : "global",
        "location" : "Authorization",
        "locationType" : "header",
        "message" : "Anonymous caller does not have storage.objects.create access to the Google Cloud Storage object.",
        "reason" : "required"
      } ],
W/System.err:   "message" : "Anonymous caller does not have storage.objects.create access to the Google Cloud Storage object."
    }
        at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:118)
        at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:37)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:532)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:455)
W/System.err:     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:565)
        at com.google.cloud.storage.spi.v1.HttpStorageRpc.create(HttpStorageRpc.java:310)
        ... 10 more

我跟踪了谷歌官方文档的文档。任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2021-01-09 12:43:37

这似乎是一个授权问题。

代码语言:javascript
运行
复制
"code" : 401,
"message" : "Anonymous caller does not have storage.objects.create access to the Google Cloud Storage object.

您可能必须检查要访问存储桶的用户的访问权限。如果您有仪表板访问权限,您可以检查特权。或者尝试使用具有gsutil的相同json证书执行相同的操作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65642693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档