2

Firebase Cloud 기능이 도입됨에 따라 현재 node.js 서버 측 코드 중 일부가 클라우드 기능으로 옮겨 가고 있습니다. 내가 가지고있는 한가지 문제점은 GCS 버킷에서 디스크의 임시 파일로 파일을 다운로드 한 다음 첨부 파일 (mailgun-js 사용)로 전자 메일로 보내는 것입니다.firebase cloud function Google Cloud Storage의 API 오류

나에게 슬픔의 원인이 코드의 일부는 다음과 같습니다

return mkdirp(tempLocalDir).then(() => { 
    const bucket = gcs.bucket(gcsBucket); 
    const tempFilePath = tempLocalDir + gcsFile; 
    return bucket.file(gcsFile).download({ 
     destination: tempFilePath 
    }).then(() => { 
     console.log('File downloaded locally to', tempFilePath); 
     var messageSubject = "Test"; 
     var messageBody = "Test with attach"; 

     var mailgunData = { 
      from: , 
      to: agentEmail, 
      subject: messageSubject, 
      html: messageBody, 
      attachment: tempFilePath, 
     }; 
     mailgunAgent.messages().send(mailgunData, function (error, body) { 
      console.log(body); 
     }); 

    }); 

}); 

나는 기능 로그에서지고있어 오류 메시지는 다음과 같습니다 I가 파일을 다운로드 할 수 있었다있어

ApiError: Forbidden 
    at Object.parseHttpRespMessage (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:156:33) 
    at Object.handleResp (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:131:18) 
    at Duplexify.<anonymous> (/user_code/node_modules/@google-cloud/storage/src/file.js:724:21) 
    at emitOne (events.js:96:13) 
    at Duplexify.emit (events.js:188:7) 
    at emitOne (events.js:96:13) 
    at DestroyableTransform.emit (events.js:188:7) 
    at emitOne (events.js:96:13) 
    at Request.emit (events.js:188:7) 
    at Request.<anonymous> (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:1108:14) 

요청을 사용하여 디스크의/tmp/폴더를 대체 할 수 있습니다.이 옵션은 대체 옵션이지만 가능한 경우 GCS 도구를 사용하고 싶습니다. 나는 GCS와의 인증 오류라고 생각하지만 그 사실을 추적하는 방법을 모르겠습니다. 파이어 폭스보다 GCS 용 클라우드 함수 .config()에서 다른 인증 매개 변수가 필요합니까? 그렇다면 어떻게 입력합니까? 우리의 GCS 버킷과 프로젝트는 Firebase Storage의 출시를 앞당기지만, 서버에서 실행되는 노드 기능과 함께 성공적으로 사용했습니다. 사전에

감사합니다, 자크는

답변

0

당신은 구글 클라우드 스토리지 API를 사용할 수있게되기 전에 식별해야합니다. 또한 다른 API도 사용할 수 있습니다.

Google 클라우드 플랫폼의 API 로그인 페이지 (here)로 이동하여 해당 서비스 계정 키를 JSON 파일로 다운로드하면됩니다. 그런 다음이 파일을 firebase 프로젝트의 함수 폴더에 넣으십시오.

마지막으로 대신 사용은 :
const bucket = gcs.bucket(gcsBucket);

이 부를 것이다 : 당신의 projectId 및 keyFilename와

const gcs = require('@google-cloud/storage'); 

const storageClient = gcs({ projectId: projectId, keyFilename: './keyFilename.json' }); const bucket = storageClient.bucket(gcsBucket);
합니다.

+0

답장을 보내 주셔서 감사합니다. 불행히도 나는 그것을 시도했지만 작동하지 않습니다. 승인 문제가있을 수 있지만 문제를 추적하는 데 어려움이 있습니다. – Zach

3

해결 : 마지막으로 위의 해결되었습니다. 문제는 @ google-cloud/storage 인증에 GCS 프로젝트 ID가 ID로 필요하지만 Firebase admin-sdk 키 파일은 GCS 프로젝트 키 파일이 아니 었습니다. Firebase 프로젝트 ID를 Firebase 키 파일이있는 GCS의 인증에서 ID로 사용하는 것도 실패했습니다.

_ (ツ) _/¯

우리의 프로젝트 설정에서 이상한 행동을 할 수 있지만, 관련 우리를 위해 문제를 해결 무엇으로 업데이트 할 생각 있습니다.

grll에서 제공하는 형식을 사용하여 일한 설정은 다음과 같습니다

const gcs = require('@google-cloud/storage'); 
const storageClient = gcs({ 
    projectId: "this is the GCS project ID, 
    keyFilename: 'this is the firebase admin-sdk keyFilename.json' 
}); 
const bucket = storageClient.bucket(gcsBucket); 
2

projectId 또는 keyFilename를 연결하지 않고도이 문제를 해결하는 또 다른 방법 :

  1. 이동 프로젝트의 중포 기지 콘솔에
  2. 설정 -> 사용 권한
  3. IAM 탭을 열어야합니다.
  4. 이제 '스토리지 관리자'역할에 대한 'App Engine 기본 서비스 계정'및 'Google API 서비스 계정'권한을 부여하십시오.
  5. 이제 괜찮을 것입니다!
+0

스토리지 관리 역할을 찾을 수 없습니다. 해당 역할에 새로운 기본 계정을 만든 다음 계정에 할당해야합니까? 또한 'Google API 서비스 계정'이 더 이상 존재하지 않습니다. – Xerri

관련 문제